An element is visible and still not able to click on it like I click on others like it

Question:

I click on a specific button on a page, but for some reason there is one of the buttons that I can’t click on, even though it’s positioned exactly like the other elements like it that I can click on.

The code below as you will notice, it opens a page, then clicks to access another page, do this step because only then can you be redirected to the real url that has the //int.

import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with open('my_user_agent.txt') as f:
    my_user_agent = f.read()
headers = {
    'User-Agent': my_user_agent
    }

options = Options()
options.set_preference("general.useragent.override", my_user_agent)
options.set_preference("media.volume_scale", "0.0")
options.page_load_strategy = 'eager'
driver = webdriver.Firefox(options=options)

today = datetime.datetime.now().strftime("%Y/%m/%d")
driver.get(f"https://int.soccerway.com/matches/{today}/")

driver.find_element(by=By.XPATH, value="//div[contains(@class,'language-picker-trigger')]").click()
time.sleep(3)
driver.find_element(by=By.XPATH, value="//li/a[contains(@href,'https://int.soccerway.com')]").click()
time.sleep(3)

try:
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@class,'tbl-read-more-btn')]")))
    driver.find_element(by=By.XPATH, value="//a[contains(@class,'tbl-read-more-btn')]").click()
    time.sleep(0.1)
except:
    pass

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-exponload='']//button[contains(@class,'expand-icon')]")))
for btn in driver.find_elements(by=By.XPATH, value="//div[@data-exponload='']//button[contains(@class,'expand-icon')]"):
    btn.click()
    time.sleep(0.1)

enter image description here

I’ve tried adding btn.location_once_scrolled_into_view before each click to make sure the button is correctly in the click position, but the problem still persists.

I also tried using the options mentioned here:

Selenium python Error: element could not be scrolled into view

But the essence of the case kept persisting in error, I couldn’t understand what the flaw in the case was.

Error text:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <button class="expand-icon"> could not be scrolled into view
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.jsm:12:1
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:192:5
ElementNotInteractableError@chrome://remote/content/shared/webdriver/Errors.jsm:302:5
webdriverClickElement@chrome://remote/content/marionette/interaction.js:156:11
interaction.clickElement@chrome://remote/content/marionette/interaction.js:125:11
clickElement@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:204:29
receiveMessage@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:92:31

Edit 1:

I noticed that the error only happens when the element is colored orange (when they are colored orange it means that one of the competition games is happening now, in other words it is live).

But the button is still the same, it keeps the same element, so I don’t know why it’s not being clicked.

See the color difference:

enter image description here

Edit 2:

If you open the browser normally or without the settings I put in my code, the elements in orange are loaded already expanded, but using the settings I need to use, they don’t come expanded. So please use the settings I use in the code so that the page opens the same.

Asked By: Digital Farmer

||

Answers:

What you missing here is to wrap the command in the loop opening those sections with try-except block.
the following code works. I tried running is several times.

import datetime
import time

from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
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
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")

caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "eager"

webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options, desired_capabilities=caps)
wait = WebDriverWait(driver, 10)

today = datetime.datetime.now().strftime("%Y/%m/%d")
driver.get(f"https://int.soccerway.com/matches/{today}/")

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class,'language-picker-trigger')]"))).click()
time.sleep(5)
wait.until(EC.element_to_be_clickable((By.XPATH, "//li/a[contains(@href,'https://int.soccerway.com')]"))).click()
time.sleep(5)

try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@class,'tbl-read-more-btn')]")))
    driver.find_element(By.XPATH, "//a[contains(@class,'tbl-read-more-btn')]").click()
    time.sleep(0.1)
except:
    pass

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@data-exponload='']//button[contains(@class,'expand-icon')]")))
for btn in driver.find_elements(By.XPATH, "//div[@data-exponload='' and not(contains(@class,'status-playing'))]//button[contains(@class,'expand-icon')]"):
    btn.click()
    time.sleep(0.1)

UPD
We need to open only closed elements. The already opened sections should be stayed open. In this case click will always work without throwing exceptions. To do so we just need to add such indication – click buttons not inside the section where status is currently playing.

Answered By: Prophet