Reputation: 989
I have the following code:
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox()
driver.get('https://www.flashscore.co.uk/tennis/atp-singles/australian-open-2021/results')
wait = WebDriverWait(driver, 5)
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
except TimeoutException:
pass
exit_loop = False
while not exit_loop:
try:
wait.until(EC.element_to_be_clickable((By.XPATH, f"//a[@class='event__more event__more--static']"))).click()
except TimeoutException:
exit_loop = True
The first try/except block is to get rid of a 'privacy' banner that causes a ElementClickInterceptedException
error and it works fine. However, I'm still getting one of the following two similar errors on the next try/except block in the while loop:
Exception has occurred: ElementClickInterceptedException
Message: Element <a class="event__more event__more--static" href="#"> is not clickable at point (623,976) because another element <span class="loadingOverlay"> obscures it
or:
Exception has occurred: ElementClickInterceptedException
Message: Element <a class="event__more event__more--static" href="#"> is not clickable at point (623,976) because another element <div id="onetrust-banner-sdk" class="otFlat ot-iab-2 bottom vertical-align-content ot-buttons-fw"> obscures it
I've tried adding another try/except block for the second error:
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='onetrust-banner-sdk']"))).click()
except TimeoutException:
pass
However this doesn't work. I suspect I can't click this element so am on the wrong track...
As neither of the elements in the errors seem to be buttons I can click - how should I deal with them?
Edit:
I have updated the code to include scrolling:
driver = webdriver.Firefox()
driver.maximize_window()
driver.get('https://www.flashscore.co.uk/tennis/atp-singles/australian-open-2021/results')
wait = WebDriverWait(driver, 5)
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
except TimeoutException:
pass
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
exit_loop = False
while not exit_loop:
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@class='event__more event__more--static']"))).click()
except TimeoutException:
exit_loop = True
However, I got each of the errors on the two runs I tried.
Upvotes: 2
Views: 490
Reputation: 29362
Jossy,
You need to scroll down till end of the page, before clicking on show more matches
code to scroll down :
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
and then you can have this code :
wait.until(EC.element_to_be_clickable((By.XPATH, f"//a[@class='event__more event__more--static']"))).click()
You should launch broswer in full screen also :
driver.maximize_window()
before loading the URL.
Update 1 :
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.flashscore.co.uk/tennis/atp-singles/australian-open-2021/results")
wait = WebDriverWait(driver, 10)
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
except TimeoutException:
pass
exit_loop = False
while not exit_loop:
try:
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
sleep(1)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@class='event__more event__more--static']"))).click()
except TimeoutException:
exit_loop = True
Upvotes: 1