当我进入google网站时,会出现这个小登录窗口:
我试图使用 selenium 和 python
来消除它这是我正在使用的代码:
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
driver = webdriver.Chrome()
driver.get('https://www.google.com.br')
driver.maximize_window()
# wait
wait = WebDriverWait(driver, 10)
not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/c-wiz/div/div/div/div[2]/div[2]/button')))
# click
not_now_button.click()
我收到以下错误:
Traceback (most recent call last):
File "C:\Users\sec-info\Desktop\python\googlr.py", line 13, in <module>
not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/c-wiz/div/div/div/div[2]/div[2]/button')))
File "C:\Users\sec-info\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
Backtrace:
(No symbol) [0x0037DCE3]
(No symbol) [0x003139D1]
(No symbol) [0x00224DA8]
(No symbol) [0x0025019F]
(No symbol) [0x002503AB]
(No symbol) [0x0027EE62]
(No symbol) [0x0026AF14]
(No symbol) [0x0027D57C]
(No symbol) [0x0026ACC6]
(No symbol) [0x00246F68]
(No symbol) [0x002480CD]
GetHandleVerifier [0x005F3832+2506274]
GetHandleVerifier [0x00629794+2727300]
GetHandleVerifier [0x0062E36C+2746716]
GetHandleVerifier [0x00426690+617600]
(No symbol) [0x0031C712]
(No symbol) [0x00321FF8]
(No symbol) [0x003220DB]
(No symbol) [0x0032C63B]
BaseThreadInitThunk [0x76CBFA29+25]
RtlGetAppContainerNamedObjectPath [0x77E57A9E+286]
RtlGetAppContainerNamedObjectPath [0x77E57A6E+238]
Python:3.11.2 硒:4.8.2 Chromedriver:111.0.5563.64
右键单击>检查
那就用最简单的css(那个xpath看起来太复杂了)
如果你可以发布 html,我可以帮忙
在 Chrome 中,按 F12 > 元素 > Ctrl+F
输入上面的CSS
button.M6CB1c.rr4y5c[aria-label='Agora não']
检查 Chrome 是否看到该元素
您可以使用 xpath 或 css 选择器来定位该特定按钮。
您需要切换到 iframe,因为您尝试单击的按钮位于 iframe 内。
使用以下代码切换到 iframe:
driver.switch_to.frame('callout')
使用xpath:
not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@class='M6CB1c rr4y5c' and @aria-label='Agora não']")))
使用CSS选择器:
not_now_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.M6CB1c.rr4y5c[aria-label='Agora não']")))
单击按钮后,不要忘记使用以下代码切换回默认内容:
driver.switch_to.default_content()
您的代码应如下所示:
driver.switch_to.frame('callout')
not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@class='M6CB1c rr4y5c' and @aria-label='Agora não']")))
not_now_button.click()
driver.switch_to.default_content()
如果您想使用 css 选择器而不是 xpath 来定位按钮,请使用如上所示的 css 定位器策略。
元素...
<iframe>
内
因此,要单击该元素以关闭弹出窗口,您必须:
诱导 WebDriverWait 使所需的 框架可用并切换到它。
诱导 WebDriverWait 使所需的元素可点击。
您可以使用以下任一定位器策略:
driver.get("https://www.google.com.br/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name=callout]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='No thanks']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
您可以在以下位置找到一些相关讨论: