Reputation: 177
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.
Upvotes: 4
Views: 6818
Reputation: 27486
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']")
Upvotes: 6