3

Selenium with Python 2.7 on Windows 10 running the Chrome driver (as a script and from the python REPL) fails to find an element by partial link text, and I'm not sure why. When I look at the source code of the webpage in question, there is only one instance of WO20 in the entire page, and it's in a link, but selenium returns no such element.

Here's an example (the WO20 comes just after the href):

<a id="resultTable:0:resultListTableColumnLink" name="resultTable:0:resultListTableColumnLink" href="detail.jsf?docId=WO2015102036&amp;recNum=1&amp;office=&amp;queryString=FP%3A%28JP2014005719%29&amp;prevFilter=&amp;sortOption=Pub+Date+Desc&amp;maxRec=3" target="_self"><span class="notranslate"> WO/2015/102036</span></a>

The page has a few other links, but the one I need is the only one with the character combination WO20, so in theory this should be easy to identify. My guess is that selenium isn't recognizing this as a link, which is why partial_link_text isn't working. I've tried using xpath (successfully), but the problem is that that returns the n-th link in the table, and the links I need (I'm processing multiple documents) aren't in a fixed position.

2
  • 1
    can you share your source code? Commented Apr 30, 2016 at 7:46
  • elem = driver.find_element_by_xpath('''//*[@id="resultTable:0:resultListTableColumnLink"]''') ... I think. I just replaced it in the script with the other answerer's suggestion, so it's gone. My original code worked, in that it identified an element, but only by position in a table, and not by the content of the link. Commented Apr 30, 2016 at 13:49

1 Answer 1

10

Actually, there is no link text that contain WO20! I think you confuse link text with href attribute of <a> element. You can find target element by link text/partial link text with following code:

driver.find_element_by_link_text('WO/2015/102036')

or

driver.find_element_by_partial_link_text('WO/20')

this XPaths should be applicable also:

driver.find_element_by_xpath('//a[contains(@href, "WO20")]')
driver.find_element_by_xpath('//a[contains(text(), "WO/20")]')
Sign up to request clarification or add additional context in comments.

2 Comments

I can't use the first one, because the numbers of the files are always different (although WO/20 is always the same), but the other three work. Thanks!
this answer is really unclear can you explain in more detail? Why is it that your solution works and his did not?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.