[英]Selenium webdriver with python can't click on a button in a webpage
until recently my code was working for scrapping the following page:直到最近,我的代码还在处理以下页面:
https://recuperarportugal.gov.pt/candidaturas-prr/ https://recuperarportugal.gov.pt/candidaturas-prr/
But now sellenium can´t click in the abertos button, id = aberto-btn但是现在 sellenium 不能点击 abertos 按钮,id = aberto-btn
I've tried with actions, xpath, selector but I do not manage to click on the button, I would appreciate any help.我已经尝试过操作 xpath 选择器,但我无法单击按钮,我将不胜感激。 Thanks谢谢
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
opciones=Options()
opciones.add_experimental_option('excludeSwitches', ['enable-automation'])
opciones.add_experimental_option('useAutomationExtension', False)
opciones.add_argument('--no-sandbox')
opciones.add_argument('--disable-dev-shm-usage')
opciones.headless=True
PATH=ChromeDriverManager().install()
URL ='https://recuperarportugal.gov.pt/candidaturas-prr/'
driver = webdriver.Chrome(executable_path='/usr/lib/chromium-browser/chromedriver',chrome_options=opciones)
driver.get(URL)
time.sleep(5)
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.find_element("id","aberto-btn").click()
I obtain the following error:我收到以下错误:
---------------------------------------------------------------------------
ElementClickInterceptedException Traceback (most recent call last)
<ipython-input-22-05303687610b> in <module>
13
14
---> 15 driver.find_element("id","aberto-btn").click()
16
17 time.sleep(2)
3 frames
/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
241 alert_text = value['alert'].get('text')
242 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here
--> 243 raise exception_class(message, screen, stacktrace)
ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (574, 983)
(Session info: headless chrome=104.0.5112.101)
Stacktrace:
#0 0x55c08f46b383 <unknown>
#1 0x55c08f23c353 <unknown>
#2 0x55c08f27b531 <unknown>
#3 0x55c08f279134 <unknown>
#4 0x55c08f27671e <unknown>
#5 0x55c08f275574 <unknown>
#6 0x55c08f269d2a <unknown>
#7 0x55c08f291db2 <unknown>
#8 0x55c08f2695c6 <unknown>
#9 0x55c08f2921de <unknown>
#10 0x55c08f2a6b9c <unknown>
#11 0x55c08f292183 <unknown>
#12 0x55c08f267bfc <unknown>
#13 0x55c08f2690c5 <unknown>
#14 0x55c08f4de510 <unknown>
#15 0x55c08f4a1eb7 <unknown>
#16 0x55c08f4a1acd <unknown>
#17 0x55c08f4a2522 <unknown>
#18 0x55c08f4dad6b <unknown>
#19 0x55c08f4a276e <unknown>
#20 0x55c08f4854e3 <unknown>
#21 0x55c08f4abcb8 <unknown>
#22 0x55c08f4abe4a <unknown>
#23 0x55c08f4c5520 <unknown>
#24 0x7fcb635486db <unknown>
You need to maximize the driver window to make that element appear inside the visible view port.您需要最大化驱动程序 window 以使该元素出现在可见视口内。
Maximize it before the driver.get(url)
command.在driver.get(url)
命令之前最大化它。 This code worked for me:这段代码对我有用:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = "https://recuperarportugal.gov.pt/candidaturas-prr/"
driver.get(url)
time.sleep(5)
driver.find_element(By.ID,"aberto-btn").click()
You created the WebDriverWait object but did not use it.您创建了 WebDriverWait object 但没有使用它。 WebDriverWait will check if the item exists every 0.5 seconds. WebDriverWait 将每 0.5 秒检查该项目是否存在。 Please give this a try.请试一试。
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='aberto-btn']"))).click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.