简体   繁体   English

无法通过 xpath 或链接文本使用 selenium python 访问链接

[英]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.这是该框代码的更广泛视图,可点击部分似乎是a标签和strong标签。 I'm not sure why xpath doesn't work but do you know how to implement find by text for the strong?我不确定为什么 xpath 不起作用,但你知道如何为强者实现文本查找吗?

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.我想单击使用上面的代码和 selenium webdriver python 编写的链接。

Does anyone know how to find the strong element using find by link text, im not sure how to implement that有谁知道如何使用通过链接文本查找来查找强元素,我不确定如何实现

XPATH: 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.要单击锚标记,请记住始终首选linkTextpartialLinkText

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium/python - “查找部分链接文本”和“xpath” - Selenium/python - “find partial link text ” and “xpath” 使用python selenium xpath查找href链接 - Finding href link using python selenium xpath selenium 无法使用方法定位元素:无法通过 id、css_selector、xpath、链接文本找到元素 - selenium Unable to locate element using methods: cannot find elements by id, css_selector, xpath, link text 'str' object is not callable error while using 'Find an element' by link text or XPATH in Selenium Python - 'str' object is not callable error while using 'Find an element' by link text or XPATH in Selenium Python 使用Selenium Webdriver和Python从XPath中提取链接? - Extract link from XPath using Selenium Webdriver and Python? python selenium - 获取链接的文本(a) - python selenium - getting the text of a link (a) 无法使用带有硒的python3在表中找到链接 - cannot locate link in table using python3 with selenium 当链接没有类时,使用Xpath在Python中获取链接的锚文本 - Using Xpath to get the anchor text of a link in Python when the link has no class 如何通过Selenium关注JavaScript链接(没有链接文字) - How to follow JavaScript link via Selenium (no link text) 在python中使用硒单击链接 - clicking a link using selenium with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM