Hello there I try to scrape this website - https://dom.ria.com/uk/realtors/ and I get a popup message below about cookies when I press accept it dismiss and I can access phone numbers but When I try to press this button using selenium I get erro ElementNotInterectable. Here is my code to handle it:
cookies = driver.find_element(By.XPATH, "//label[@class='button large']")
driver.implicitly_wait(20)
cookies.click()
I tried to use driver.implicitly_wait()
but it still doesn't work.
How can I fix this?
CodePudding user response:
Your xpath matches two elements on the page. In this case, selenium simply grabs the first element, which does not happen to be the one that you want. Try something like this:
cookies = driver.find_elements(By.XPATH, "//label[@class='button large']")
# wait if you have to
cookies[1].click()
CodePudding user response:
A reliable way of accepting cookies on that page would be:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
### other imports, setting up selenium, etc ##
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
url = 'https://dom.ria.com/uk/realtors/'
browser.get(url)
try:
wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@]/label'))).click()
print('accepted cookies')
except Exception as e:
print('no cookie button!')
Selenium docs can be found at https://www.selenium.dev/documentation/