我是Selenium的新手,我尝试点击第二个页面上的链接(登录页面之后)。登录部分工作正常,浏览器进入下一个页面,但在下一个页面上,它引发了一个异常:
这是我的回溯:
Traceback (most recent call last):
File "d:/Personal_projects/Programmeren/Python/Automate the boring stuff/Chapter12/seleniumTest.py", line 22, in <module>
weekElem = browser.find_element(By.PARTIAL_LINK_TEXT,'/go/')
File "C:\Users\****\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 1246, in find_element
'value': value})['value']
File "C:\Users\****\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "C:\Users\****\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"/go/"}
(Session info: chrome=96.0.4664.93)
这是我的代码(登录信息明显模糊):
#! python3
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
browser = webdriver.Chrome()
# First webpage
browser.get('https://www.signupgenius.com/register')
userElem = browser.find_element(By.ID,'email')
userElem.send_keys('************')
passwordElem = browser.find_element(By.ID,'pword')
passwordElem.send_keys('**********')
loginBtnElem = browser.find_element(By.ID,'loginBtnId')
loginBtnElem.click()
time.sleep(10)
# Second webpage
weekElem = browser.find_element(By.PARTIAL_LINK_TEXT,'/go/')
weekElem.click()
我要查找的元素是:
<a href="/go/10C426245672F85" class="text-orange text-underlined"><strong class="ng-binding">1212</strong></a>
(the part after /go/每周都会更改,这就是为什么我使用部分链接文本的原因)
我希望有人能帮我解决这个问题!
编辑我找到了修复方法,通过使用Xpath解决了问题:
weekElem = browser.find_element(By.XPATH, '//a[contains(@href,"/go/")]')
如果有人能详细说明为什么部分链接文本不起作用,那将会给予我更深入地了解 selenium 是如何工作的!
2条答案
按热度按时间x33g5p2x1#
字符串***go***不是 A 标记的 LINK_TEXT 的一部分,而是
href
属性值的一部分。因此,不能将***go***用作 PARTIAL_LINK_TEXT。溶液
可以使用字符串***go***作为
href
属性值的一部分,如下所示:starts-with()
:contains()
:6yt4nkrj2#
使用下面的代码并将“变量”替换为whatever,将“链接”替换为链接中的单词'''variable = WebDriverWait(driver,15).until(错误代码.element_to_be_clickable((By.XPATH,'.//a[contains(@href,“/link/”)]/span '))).click()
a[contains(@href,“/jobs/”)]/span'''如果那不工作右键单击网页,单击检查然后使用箭头图标单击他们想要交互的按钮。在那之后右键单击按钮的源代码并悬停在复制上然后单击复制xpath或任何需要的