until recently my code was working for scrapping the following page:
https://recuperarportugal.gov.pt/candidaturas-prr/
But now sellenium can´t click in the abertos button, 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. 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>
CodePudding user response:
You need to maximize the driver window to make that element appear inside the visible view port.
Maximize it before the driver.get(url)
command.
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()
CodePudding user response:
You created the WebDriverWait object but did not use it. WebDriverWait will check if the item exists every 0.5 seconds. Please give this a try.
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='aberto-btn']"))).click()