2

I have a popup with list of names. I need to choose "mahmoud" for instance by clicking the "select" link beside the label "mahmoud". How to click that select? I am using selenium webdriver with python.

# these 2 code lines are not working
driver.find_element_by_name('123')
driver.find_element_by_link_text('Select').click()

name
 - mahmoud   select
 - ahmed     select 
 - engy      select

# mahmoud is text,select is link

The HTML code for the first row is:

<tr class="gridRow">
    <td>1</td>
    <td>Mahmoud</td>
    <td>
    <a href="javascript:void(0)">Select</a>
    </td>
</tr> 
5
  • Can you rewrite the question in English, please?
    – user246
    Oct 9, 2012 at 16:11
  • 2
    Tried to edit, but gave up half way. Absolutely can't understand the question. Please break down your question into smaller statements. Oct 9, 2012 at 16:41
  • Finally edited, hopefully it doesn't change the original question Oct 10, 2012 at 18:38
  • @engy, welcome to SQA. Assuming Suchit edited your question correctly, we cannot answer your question unless we know how your HTML is structured: specifically, what the elements look like in the vicinity of each name and select link/button.
    – user246
    Oct 11, 2012 at 14:45
  • Need some more of the html. Please provide the parent tag as that would be very useful in this case e.g. <table> tag and possibly the one above that too. Oct 15, 2012 at 16:24

5 Answers 5

3

Use XPATH. Install a tool like FirePath to help yourself debug this, but you'll probably want something like:

select_finder = "//tr[contains(text(), 'Mahmoud')]//a"
driver.find_element_by_xpath(select_finder).click()

where the XPATH reads something like "find a table row which contains the text "Mahmoud", then find a hyperlink inside that row"

6
  • I think he/she is looking to click the "select" next to mahmoud, and seems like "mahmoud" is not a link Oct 10, 2012 at 16:15
  • yeah, i think the xpath would work as long as the li contained both the text "mahmoud" and a link...
    – hwjp
    Oct 10, 2012 at 16:55
  • That's my point. If the label and the select are separate elements, which it seems from the question it won't work. Oct 10, 2012 at 17:19
  • I think we agree with each other - we can't tell unless we know the precise structure of the HTML the OP is using. Still, if it's a UL/LI structure, I figure it's pretty likely the LI will contain both the text and the hyperlink, so my structure would work. but it's only an example...
    – hwjp
    Oct 10, 2012 at 22:05
  • hi all sorry for my poor english here is the HTML code for my question: <tr class="gridRow"><td>1</td> <td>Mahmoud</td> <td> <a href="javascript:void(0)">Edit</a> <a href="javascript:void(0)">Select</a> </td> </tr>
    – engy
    Oct 15, 2012 at 12:13
0

If I understand it correctly, you should look for the parent element of that row. If it is in a table, then you can get the row number in which your element resides and use that. I won't (and can't without the actual html) provide an answer, but these are the things you can do (if you haven't already):

  • Read about using XPaths here - http://www.w3.org/TR/xpath/. Section 2.5 Abbreviated Syntax has been of great value to me personally
  • Read about using XPaths with Selenium
  • Find the XPath of the label e.g. Mahmoud. Now that you know the row and column for the label, you can find the relative position of the corresponding "Select"
1
  • i'm using firebug with firefox to get the element's XPATH but because the table has like 5 rows each row has select link in it my question was how to choose a specific link in specific row "find_element_by_text_link" not working.
    – engy
    Oct 15, 2012 at 12:18
0

Another option would be to set a unique identifier for each of the links. That way, if you're looking for the select for mahmoud, you could set an id, such as <a id="mahmoud">

0

Thanks for all who tried to help me with my question.

Actually I solved it using the driver.find_element_by_xpath method and because it's a popup it needed the driver.implicitly_wait(10) method until the page rendered the popup and voila, it worked.

I found the clues at Stack Overflow website.

0

This relative xpath locator should also work for the above html

//td[text()="Mahmoud")]/../td[3]/a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.