我试图让Selenium在谷歌上搜索点击,但它总是给我一个错误


from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="Driver\chromedriver.exe")
driver.implicitly_wait(10)
driver.get("https://www.google.com/")
driver.find_element(By.XPATH, "//button[contains(text(),'No thanks')]").click()
driver.switch_to.frame(driver.find_element(By.XPATH, "//iframe[@name='account']"))
driver.find_element(By.CSS_SELECTOR, "input[title='Search']").send_keys("google")

上面是我想写的剧本。我没有得到任何这样的元素异常粘贴了下面的错误消息。

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(text(),'No thanks')]"}
(Session info: chrome=104.0.5112.81)
Stacktrace:
Backtrace:
Ordinal0 [0x00E878B3+2193587]

我尝试链接"https://www.google.com">

它只是显示";不,谢谢;按钮

也许你可以用";http://www.google.com"以进行搜索,并按如下所示删除该行。

driver.find_element(By.XPATH, "//button[contains(text(),'No thanks')]").click()

顺便说一句,如果你想做一个搜索功能。我认为谷歌搜索API是另一种方法。

相关内容

最新更新