Selenium 无法单击按钮,因为有 div 覆盖
Selenium cannot click button because there is a div overlay
我正在尝试删除此网页 https://www.tecnocasa.es/venta/piso/barcelona/barcelona/510567.html,我使用的代码如下,我认为是正确的:
options = Options()
options.headless = False
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe')
driver.get(url)
# ("Headless Firefox Initialized")
#dins del id= cookie-banner
frame = driver.find_element(by=By.CSS_SELECTOR,value = '[id = "cookie-banner"]')
print(frame)
buttons = frame.find_elements(by=By.TAG_NAME, value='button')
print(buttons)
buttons[0].click()
time.sleep(5)
找到了按钮,但是当我尝试单击其中一个时出现错误 <button class="btn-default"> is not clickable at point (1138,829) because another element <div id="hide-overlay" class="hide-overlay fade-leave-active fade-leave-to"> obscures it
如何禁用此叠加层并单击按钮接受页面条件?
你的代码在我的电脑上没有任何问题,试试这个
driver.find_element(By.CSS_SELECTOR, '#close').click()
如果这也行不通,您可以试试这个隐藏横幅的代码。但是,每次加载新页面时,您都应该 运行 它
overlay = driver.find_element(By.CSS_SELECTOR, 'div.cookies-overlay')
driver.execute_script("arguments[0].style.display = 'none';", overlay)
banner = driver.find_element(By.CSS_SELECTOR, '#cookie-banner')
driver.execute_script("arguments[0].style.display = 'none';", banner)
我正在尝试删除此网页 https://www.tecnocasa.es/venta/piso/barcelona/barcelona/510567.html,我使用的代码如下,我认为是正确的:
options = Options()
options.headless = False
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe')
driver.get(url)
# ("Headless Firefox Initialized")
#dins del id= cookie-banner
frame = driver.find_element(by=By.CSS_SELECTOR,value = '[id = "cookie-banner"]')
print(frame)
buttons = frame.find_elements(by=By.TAG_NAME, value='button')
print(buttons)
buttons[0].click()
time.sleep(5)
找到了按钮,但是当我尝试单击其中一个时出现错误 <button class="btn-default"> is not clickable at point (1138,829) because another element <div id="hide-overlay" class="hide-overlay fade-leave-active fade-leave-to"> obscures it
如何禁用此叠加层并单击按钮接受页面条件?
你的代码在我的电脑上没有任何问题,试试这个
driver.find_element(By.CSS_SELECTOR, '#close').click()
如果这也行不通,您可以试试这个隐藏横幅的代码。但是,每次加载新页面时,您都应该 运行 它
overlay = driver.find_element(By.CSS_SELECTOR, 'div.cookies-overlay')
driver.execute_script("arguments[0].style.display = 'none';", overlay)
banner = driver.find_element(By.CSS_SELECTOR, '#cookie-banner')
driver.execute_script("arguments[0].style.display = 'none';", banner)