1

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.

Digital Farmer
  • 1,705
  • 5
  • 17
  • 67
  • Those element is already expanded, then how you expecting those element will expand again – KunduK Oct 31 '22 at 15:11
  • Hi @KunduK When they are expanded, it is ignored by the code (```//div[@data-exponload='']```), maybe in your browser they are showing expanded, but here using the code and running it, they don't appear expanded. See the image (i edited the question) – Digital Farmer Oct 31 '22 at 15:15
  • hi @KunduK below the code I put a GIF demonstrating exactly where the code stops executing and delivers the error, in video it is better to visualize. – Digital Farmer Oct 31 '22 at 15:26
  • Yes, For me it appeared expanded, both chrome and firefox – KunduK Oct 31 '22 at 15:36
  • @KunduK I did a test, opening it manually (myself opening the browser and accessing the site) they actually appear expanded, but when I run with the code model (where I disabled some things to make it load faster), they appear closed. – Digital Farmer Oct 31 '22 at 15:49

1 Answers1

1

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:\webdrivers\chromedriver.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.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Hello @Prophet , thank you for your attention and for taking the time to help me, but this does not solve the problem. Because in this case, it skips the "orange" buttons and keeps them ocult, I need to expand them, not ignore them. – Digital Farmer Oct 31 '22 at 18:02
  • OK, see my updated answer. it opens all the closed sections while leaving open already opened sections. This approach do not need `try-except` block. Tested. Working :) – Prophet Oct 31 '22 at 18:41
  • ```//div[@data-exponload='']``` already does this process of ignoring those that are expanded by default, the problem is those that are closed but are in orange color (```status-playing```), but in Firefox and using the settings I put to open the page more fast, they don't come expanded by default, maybe in Chrome it does. In this case, unfortunately, your option still does not solve the problem. But again, thank you for your time and willingness to help me. – Digital Farmer Oct 31 '22 at 19:10
  • 1
    have you **tried** my code? I tried your code and it **does not** ignore already opened sections. But with my updated code it actually does. – Prophet Oct 31 '22 at 21:01
  • Ok ok, now I understand the point of the problem, I was not able to understand the problem itself! I had used the code but using the page already opened it actually kept the problem, but now opening the page completely clean works perfectly, thanks! – Digital Farmer Oct 31 '22 at 21:13