【发布时间】:2023-03-23 18:42:02
【问题描述】:
我是网络爬虫任务的新手。之前我试过下面这个简单的爬虫,效果很好。 最近我回到代码并尝试在爬虫上做更多事情,但是 browser.find_element_by_id("lst-ib") 不起作用,我收到错误提示
' 没有这样的元素:无法找到元素:{"method":"css selector","selector":"[id="lst-ib"]"} (会话信息:chrome=84.0.4147.89)'
为了解决我的问题,我尝试从检查中找到谷歌页面的输入文本框的 xpath。总是这样吗?我们为爬虫定义的 id 或 css 选择器是否会定期更改,我们应该更新代码吗?
from selenium import webdriver
url = "https://www.google.com"
browser = webdriver.Chrome(executable_path = "chromedriver")
browser.get(url)
#inputElement = browser.find_element_by_id("lst-ib")
# I replace the xpath with previous id
inputElement =
browser.find_element_by_xpath("/html/body/div/div[2]/form/div[2]/div[1]/div[1]/div/div[2]/input")
inputElement.send_keys("my input search text")
inputElement.submit()
browser.quit()
【问题讨论】:
-
试试
inputElement = browser.find_element_by_xpath('//input[@title="Search"]') -
@JaSON 我仍然收到此错误:无法找到元素:{"method":"xpath","selector":"//input[@title="Search"]"} (Session信息:铬=84.0.4147.89)。我做错了什么吗?
-
this discussion 对您有帮助吗?
-
@Mahsa ,然后尝试申请ExplicitWait
标签: css selenium selenium-webdriver web-crawler