Billy
Billy

Reputation: 1

Cannot accessing link using selenium python via xpath or link text

The Webpage

<div class="span3"> <div class="stat-panel"> <div class="stat-cell1 bg-stats1 valign-middle" style="text-align: center;"> <a href="co-op/coopjobs.htm"><strong>View and Apply for<br> Co-op Jobs</strong></a> </div> </div> </div>

This is a broader view of the code of that box, The clickable part seems to be the a tag and the strong tag. I'm not sure why xpath doesn't work but do you know how to implement find by text for the strong?

I'm not sure how to access the link above. I want to click on a link that is written using the code above with selenium webdriver python.

Does anyone know how to find the strong element using find by link text, im not sure how to implement that

Upvotes: 0

Views: 85

Answers (2)

Prophet
Prophet

Reputation: 33351

This should work:

driver.find_element_by_xpath("//a[contains(@href,'co-op/coopjobs.htm')]").click()

If this doesn't work for you we need more information.
So, the above locator is not a unique.
You need to use the following:

driver.find_element_by_xpath("//a[contains(@href,'co-op/coopjobs.htm') and(contains(text(),'View and Apply for'))]").click()

Try this:

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_xpath("//a[contains(@href,'co-op/coopjobs.htm') and(contains(text(),'View and Apply for'))]")

actions = ActionChains(driver)
actions.move_to_element(element).perform()
element.click()

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29362

XPATH :

//a[contains(@href, "co-op/coopjobs.htm")]

and click on it like this :

driver.find_element_by_xpath("//a[contains(@href, "co-op/coopjobs.htm")]").click()

for clicking on anchor tag, remember always linkText or partialLinkText is preferred.

Upvotes: 1

Related Questions