I have href value of an anchor tag which only have href value as attribute. Now I want to find the element in the page which have same value as my href value and click it. I am unable to find any way of doing this using standard selenium methods.How can I do this? Basically these are the functions I found but it seems that I can't use any of these:
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
To identify the link with the href attribute we can take the help of the locators xpath or css selector. We shall use the findElement method and pass By. xpath or By. cssSelector as a parameter to this method.
We can find an element using the link text or the partial link text in Selenium webdriver. Both these locators can only be applied to elements with the anchor tag. The link text locator matches the text inside the anchor tag.
We can fetch href links in a page in Selenium by using the method find_elements(). All the links in the webpage are designed in a html document such that they are enclosed within the anchor tag. To fetch all the elements having <anchor> tagname, we shall use the method find_elements_by_tag_name().
You can use find_element_by_xpath functionality.
driver.find_element_by_xpath('//a[@href="'+url+'"]')
You can try this:
driver.find_element_by_xpath('//a[contains(@href,"href")]')
You would find the element by the CSS selector, as you would using vanilla CSS:
link = driver.find_element_by_css_selector('[href^=http://somelink.com/]')
You can also find the element by the link text:
link = driver.find_element_by_partial_link_text('somelink')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With