4

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.

1 Answer 1

6

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']")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you. I think this will help greatly.

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.