Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Does anyone know how you can click on a link based on the href using the python webdriver bindings. In waitr-webdriver you can do something like this:

browser.link(:href => "http://www.testsite.com/pageOne.html").click

But I haven't be able to find a similar function in the python webdriver. All there is are

find_element_by_class_name
find_element_by_css_selector
find_element_by_id
find_element_by_link_text
find_element_by_name
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_xpath

These are great methods but the site I am testing has no ID's or Classes in their links. So the only unique thing about the links are the href url.

Any help would be greatly appreciated.

share|improve this question

1 Answer

up vote 2 down vote accepted

Under the covers, watir-webdriver is converting the call to find the link where the href attribute is a given value into a WebDriver find-by-XPath expression. Mimicing this in your own code, the appropriate way to handle this in Python would be:

# assume driver is an instance of WebDriver
# NOTE: this code is untested
driver.find_element_by_xpath(".//a[@href='http://www.testsite.com/pageOne.html']")

Alternatively, you could use CSS selectors (which would be faster on IE) as follows:

# again, assume driver is an instance of WebDriver
# NOTE: this code is untested
driver.find_element_by_css_selector("a[href='http://www.testsite.com/pageOne.html']")
share|improve this answer
Thanks you. I think this will help greatly. – sunnysidesounds Jul 12 '11 at 22:03
Any chance you'd like to mark this as an accepted answer? – JimEvans Jul 13 '11 at 13:34
No problem at all – sunnysidesounds Jul 13 '11 at 18:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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