【发布时间】:2020-08-19 19:33:23
【问题描述】:
【问题讨论】:
-
请阅读为什么screenshot of HTML or code or error is a bad idea。考虑使用基于格式化文本的相关 HTML、代码试验和错误堆栈跟踪来更新问题。
标签: python selenium xpath css-selectors webdriverwait
【问题讨论】:
标签: python selenium xpath css-selectors webdriverwait
要单击文本为 Lukk 的元素,您需要将WebDriverWait 诱导为element_to_be_clickable(),您可以使用以下任一Locator Strategies:
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ytcp-button#close-button div[label='Lukk']"))).click()
使用XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ytcp-button[@id='close-button']//div[@label='Lukk' and text()='Lukk']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
【讨论】:
我会说首先等待弹出窗口打开。显式等待可以做到:
这是一个示例代码: https://seleniumbyexamples.github.io/waitvisibility
您的选择器将是 paper-dialog#dialog
一旦弹出窗口可见,您现在就可以等待按钮了。
您可以使用等待可点击:
https://seleniumbyexamples.github.io/waitclickable
id 是关闭按钮
您也可以尝试其他定位器:
【讨论】: