gpt4 book ai didi

python - 使用 Python 在 .submit() 之后搜索 Selenium 中的错误元素

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:13 25 4
gpt4 key购买 nike

我有一些 Selenium 代码,可以使用以下代码将各种搜索词输入到网站的搜索字段中:

browser = webdriver.Chrome()
browser.get(url)

search_box1 = browser.find_element_by_id('searchText-0')
search_box2 = browser.find_element_by_id('searchText-2-dateInput')

search_box1.send_keys("Foobars")
search_box1.send_keys("2013")

search_box1.submit()

然后我编写了更多代码来获取给定搜索查询结果的点击次数。然而,对于特定年份的“Foobars”的某些值,没有命中,查询结果如下所示:

<body class="search">
<div id="skip">...</div>
<div style = "display:non;">...</div>
<div id="container" class="js">
<div id="header">...</div>
<div id="search">...</div>
<div id="helpContent">...</div>
<div id="main-body" class="noBg">
<div class="error">
<div>Sorry. There are no articles that contain all the keywords you entered.</div>
<p>Possible reasons:
</p>
<ul></ul>
<p></p>
<p></p>
</div>
</div>

当搜索查询命中时,如何检查搜索查询是这个而不是我得到的页面?我打算实现一个 if 语句来检查搜索查询是否返回了某些内容,但我似乎无法找出正确的语法来获取我需要执行此操作的错误元素。我尝试过这样的事情:

Error=browser.find_element_by_name('error')

Error=browser.find_element_by_xpath("//div[@class='error']")

但我不断收到错误:

selenium.common.exceptions.NoSuchElementException: Message: u'no such element\n

我想识别错误元素,以便我可以执行类似的操作

if Error == "There are no articles that contain all the keywords you entered":
do something
else:
do something else

或者更好的是,可以告诉我是否存在用于条件的错误。任何帮助将不胜感激。

最佳答案

也许你来得太快了?尝试一下

from selenium.webdriver.support import expected_conditions as EC
....
Error= WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@class='error']")))

关于python - 使用 Python 在 .submit() 之后搜索 Selenium 中的错误元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20282201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com