Note that there are some explanatory texts on larger screens.

plurals
  1. POGet the ID of an element using Selenium Web Driver in PHP
    text
    copied!<p>I am building an application in PHP/MySQL/jQuery and it displays a list of items to the user, with a blank field at the bottom to add a new item. There's an Ajax blur event that saves your data to the database, then replaces the "new" row with a regular row, which includes the text you entered and an edit button. If you click the edit button, you it turns the text back into an input. All of these have IDs like item_n and edit_n where n is the ID in the database. What I'm really after is n.</p> <p>Using Selenium (I've been focusing on Web Driver, but I'm open to using IDE), I'd like to create a new item, then click the edit button and alter it, but I don't know the ID of the edit button.</p> <p>I've been able to create tests where I create a new row, then confirm that it's there using assertTextPresent, but I can't find a way to get the ID of the new row (which will be item_n).</p> <p>I've tried using :contains, but it always returns an empty string. I've tried a few CSS and Xpath selectors, ranging from general to specific:</p> <pre><code>$elem = $this-&gt;_session-&gt;element('css', 'div.item_row div.item_title:contains("'.$new_item.'")'); $elem = $this-&gt;_session-&gt;element('xpath', '//div:contains("'.$new_task.'")'); </code></pre> <p>I also tried just getting the second to last element in the list (since the last is the new-row field), but I'd really prefer to not have to rely on sorting.</p> <p>The question below indicates Selenium RC could call JavaScript: <a href="https://stackoverflow.com/questions/2959188/selenium-rc-how-to-check-if-an-element-has-a-given-attribute">Selenium RC: How to check if an element has a given attribute?</a></p> <p>I know Web Driver succeeded Selenium RC, but I'm not sure if Web Driver supports RC commands such as the above. That said, I gave it a try anyway. The JavaScript function I wrote to get the ID returns null. I did test the function outside of Selenium, so I know it's working.</p> <p>I'm using the Facebook PHP Web Driver implementation, but I'm not married to it: <a href="https://github.com/facebook/php-webdriver" rel="nofollow noreferrer">https://github.com/facebook/php-webdriver</a></p> <p>Any help would be much appreciated. Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload