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]
发布于 2022-08-17 00:52:19
我试试"https://www.google.com"“的链接。
它只是在第一次显示“不谢谢”按钮。
也许您可以使用"http://www.google.com“搜索,并删除如下行。
driver.find_element(By.XPATH, "//button[contains(text(),'No thanks')]").click()
顺便说一句,如果你想做一个搜索功能。我认为google搜索API是另一种方法。
https://stackoverflow.com/questions/73384963
复制