代码无法在切换的弹出窗口中定位元素
Code can not locate elements on switched popup
我无法点击弹出 iframe 上的同意按钮。我测试了元素和硒库的多种方法。代码returns错误:
"Message: no such element: Unable to locate element: {"method":"css
selector","selector":"[id="onetrust-accept-btn-handler"]"} (Session
info: chrome=88.0.4324.150)"
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
import time
driver = webdriver.Chrome()
driver.get("https://skelbiu.lt")
driver.implicitly_wait ( 30 )
driver.maximize_window ()
WebDriverWait(driver, 60).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"lsgetframe")))
try:
driver.find_element_by_xpath("//button[@id='onetrust-accept-btn-handler']").click()
except NoSuchElementException as e:
print("e 1", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 2", e)
try:
driver.find_element_by_xpath("//div[@id='onetrust-button-group']/button").click()
except NoSuchElementException as e:
print("e 3", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "//div[@id='onetrust-button-group']/button"))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 4", e)
try:
driver.find_element_by_xpath("//div[2]/div/button").click()
except NoSuchElementException as e:
print("e 5", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "//div[2]/div/button"))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 6", e)
try:
driver.find_element_by_id("onetrust-accept-btn-handler").click()
except NoSuchElementException as e:
print("e 7", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 8", e)
try:
driver.find_element_by_css_selector("button#onetrust-accept-btn-handler").click()
except NoSuchElementException as e:
print("e 9", e)
time.sleep(5)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 10", e)
try:
driver.find_element_by_xpath('//*[@id="onetrust-accept-btn-handler"]').click()
except NoSuchElementException as e:
print("e 11", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, '//*[@id="onetrust-accept-btn-handler"]'))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 12", e)
try:
driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[1]/div/div[2]/div/button[1]').click()
except NoSuchElementException as e:
print("e 13", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[10]/div[2]/div[1]/div/div[2]/div/button[1]'))).click()
except NoSuchElementException as e:
print("e 14", e)
元素 Sutinku 不在任何 <iframe>
中。但是元素是一个动态元素,所以理想情况下,点击你需要诱导的元素 WebDriverWait for the and you can use either of the following :
使用CSS_SELECTOR
:
driver.get("https://www.skelbiu.lt/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
使用XPATH
:
driver.get("https://www.skelbiu.lt/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Sutinku']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于 的一些相关讨论:
我无法点击弹出 iframe 上的同意按钮。我测试了元素和硒库的多种方法。代码returns错误:
"Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="onetrust-accept-btn-handler"]"} (Session info: chrome=88.0.4324.150)"
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
import time
driver = webdriver.Chrome()
driver.get("https://skelbiu.lt")
driver.implicitly_wait ( 30 )
driver.maximize_window ()
WebDriverWait(driver, 60).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"lsgetframe")))
try:
driver.find_element_by_xpath("//button[@id='onetrust-accept-btn-handler']").click()
except NoSuchElementException as e:
print("e 1", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 2", e)
try:
driver.find_element_by_xpath("//div[@id='onetrust-button-group']/button").click()
except NoSuchElementException as e:
print("e 3", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "//div[@id='onetrust-button-group']/button"))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 4", e)
try:
driver.find_element_by_xpath("//div[2]/div/button").click()
except NoSuchElementException as e:
print("e 5", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "//div[2]/div/button"))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 6", e)
try:
driver.find_element_by_id("onetrust-accept-btn-handler").click()
except NoSuchElementException as e:
print("e 7", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 8", e)
try:
driver.find_element_by_css_selector("button#onetrust-accept-btn-handler").click()
except NoSuchElementException as e:
print("e 9", e)
time.sleep(5)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 10", e)
try:
driver.find_element_by_xpath('//*[@id="onetrust-accept-btn-handler"]').click()
except NoSuchElementException as e:
print("e 11", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, '//*[@id="onetrust-accept-btn-handler"]'))).click()
except (TimeoutException, NoSuchElementException) as e:
print("e 12", e)
try:
driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[1]/div/div[2]/div/button[1]').click()
except NoSuchElementException as e:
print("e 13", e)
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[10]/div[2]/div[1]/div/div[2]/div/button[1]'))).click()
except NoSuchElementException as e:
print("e 14", e)
元素 Sutinku 不在任何 <iframe>
中。但是元素是一个动态元素,所以理想情况下,点击你需要诱导的元素 WebDriverWait for the
使用
CSS_SELECTOR
:driver.get("https://www.skelbiu.lt/") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
使用
XPATH
:driver.get("https://www.skelbiu.lt/") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Sutinku']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于