My code is encountering the issue of "element click intercepted" when not using WebDriverWait. On the other hand, when utilizing WebDriverWait, the error message states that 'None

Code Proposal:

To simplify the process of gathering links to all games on a given day from the page , I am looking to create a script that allows me to dynamically change the date, such as 2021/08/01 or any other date. This way, I can easily loop through and gather information from multiple days with a single code execution.

Despite being a slow approach without using Headless, the current model successfully clicks all buttons, expands the data, and imports all 465 listed match links:

for btn in driver.find_elements_by_xpath("//tr[contains(@class,'group-head  clickable')]"):
    btn.click()

Full Code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(r"C:\Users\Computador\Desktop\Python\chromedriver.exe", options=options)

url = "https://int.soccerway.com/matches/2021/07/28/"

driver.get(url)
driver.find_element_by_xpath("//div[@class='language-picker-trigger']").click()
driver.find_element_by_xpath("//a[@href='https://int.soccerway.com']").click()
time.sleep(10)
for btn in driver.find_elements_by_xpath("//tr[contains(@class,'group-head  clickable')]"):
    btn.click()
time.sleep(10)
jogos = driver.find_elements_by_xpath("//td[contains(@class,'score-time')]//a")
for jogo in jogos:
    resultado = jogo.get_attribute("href")
    print(resultado)
driver.quit()

However, when adding

options.add_argument("headless")
to prevent the browser from opening on screen, an error is encountered:

Message: element click intercepted

In order to address this issue, further analysis led to the use of WebDriverWait (). An attempt was made to resolve it by applying the following syntax:

for btn in WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, "//tr[contains(@class,'group-head  clickable')]"))):
    btn.click()

Full Code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

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")
options.add_argument("headless")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(r"C:\Users\Computador\Desktop\Python\chromedriver.exe", options=options)

url = "https://int.soccerway.com/matches/2021/07/28/"

driver.get(url)
driver.find_element_by_xpath("//div[@class='language-picker-trigger']").click()
driver.find_element_by_xpath("//a[@href='https://int.soccerway.com']").click()
time.sleep(10)
for btn in WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, "//tr[contains(@class,'group-head  clickable')]"))):
    btn.click()
time.sleep(10)
jogos = driver.find_elements_by_xpath("//td[contains(@class,'score-time')]//a")
for jogo in jogos:
    resultado = jogo.get_attribute("href")
    print(resultado)
driver.quit()

Unfortunately, this implementation returns an error due to 'NoneType' object not being iterable.

'NoneType' object is not iterable

Reason for the Option:

1 - The need for running automation in an online terminal without displaying the browser window on screen, optimizing processing speed within terminal constraints.

2 - Flexibility to input different dates instead of hardcoded values like 2021/07/28 in the URL:

url = "https://int.soccerway.com/matches/2021/07/28/"

Future integration of a variable for date selection:

today = date.today().strftime("%Y/%m/%d")

In search of faster alternatives, referenced methods may present improved efficiency while avoiding the requirement of WebDriver. However, challenges persist when attempting to scale beyond the default date provided.

The anticipated outcome (with limited representation due to character constraints) includes 465 game links:

https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/fc-sheriff-tiraspol/alashkert-fc/3517568/
https://int.soccerway.com/matches/2021/07/28/europe/uefa-champions-league/fk-neftchi/olympiakos-cfp/3517569/        
...
https://int.soccerway.com/matches/2021/07/28/africa/cecafa-senior-challenge-cup/uganda-under-23/eritrea-under-23/3567664/

Note 1: Various types of score-time classes exist, including score-time status and score-time score, thus necessitating the usage of contains in

"//td[contains(@class,'score-time')]//a"

Update

In addition to addressing the current challenge, exploring enhanced and swifter options for improving the existing method would be greatly valued as part of my ongoing learning process. (Continuously seeking advancements beyond dated methodologies).

Answer №1

FC Copenhagen,

I have identified two issues in your code

The first problem lies here:

for btn in WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//tr[contains(@class,'group-head  clickable')]"))):
    btn.click()

This is incorrect because the element_to_be_clickable function will return a single web element, resulting in a <non-iterable error>. Instead, we should use

visibility_of_all_elements_located
which returns a list of elements.

Secondly, you cannot directly perform a click action on some elements as they may not be within the Selenium viewport. To resolve this, we need to utilize ActionsChain

Refer to the updated code snippet below :

Updated code snippet with required changes...

Output after making necessary adjustments :

List of output data generated based on the request made in the script

Answer №2

Why Selenium Isn't Always Necessary

Using Selenium as the main tool for web scraping is not ideal. It tends to be slower and requires more code compared to other alternatives. Whenever possible, opt for using requests along with the lxml parser instead. In cases where Selenium is only being used to switch between URLs, this can easily be hardcoded, eliminating the need for it altogether.

(Code Snippet Omitted)

When Does Selenium Come in Handy?

In today's web landscape, many websites serve dynamic content. If the data you're after isn't statically loaded:

  1. Inspect the browser's network tab to identify relevant requests,
  2. Try replicating these requests using requests.

If #1 and #2 aren't feasible due to site design constraints, resorting to selenium may be necessary. This tool mimics user interactions to access the required content. When parsing HTML, you can still choose lxml, or remain within the selenium environment which also offers similar functionalities.

First revision highlights:

  • Addressed issues raised by OP
  • Outlined limitations of provided code
  • Conducted code restructuring
  • Implemented date verification for saving only matches played on the specified date
  • Added feature for saving search results

Second revision includes:

  • Enhanced functionality for navigating across all pages of listed competitions utilizing get_page_params() and request_other_pages()
  • Further refinements in the code base

Answer №3

Experiment with incorporating the Settings screen-dimensions Minus the use of WebDriverWait

options.add_argument("screen-dimensions=1440,900")

Output

https://i.stack.imgur.com/xPn83.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the World of Buttons and Canvas in tkinter

My code is intended to display a keyboard design with 4 rows and 16 columns. https://i.stack.imgur.com/Guu5r.png In the provided image, there seems to be an issue with one of the buttons ('A') missing from its expected position in the top right ...

Encountering an error while trying to install PyDev in Eclipse

There was an issue when trying to access the repository at http://pydev.sf.net/updates/content.xml. sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid c ...

displaying outcomes as 'Indefinite' rather than the anticipated result in the input field

I need to automatically populate values into 4 text fields based on the input value entered by the user. When the user exits the input field, a function called getcredentials() is triggered. This function, in turn, executes Python code that retrieves the r ...

Inquiry on Selenium Python Web Scraping

I encountered an issue with my code : from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver import EdgeOptions import os os.environ['PATH'] += & ...

Determining the semester of a day using pandas.Period: A step-by-step guide

Is there an easy way to determine which semester a given day belongs to and display it in the format 'YYYY-SX'? For example, turning 2018-01-01 into (2018S1). While it's simple to do this for quarters with a date range using pandas: import ...

Transitioning from an iFrame back to the Parent Frame and locating an element within the Parent Frame using Selenium Webdriver in C#

Situation: - On a page with an iFrame Text Editor and a button, I need to read from the Text Editor body within the iFrame. - Once done reading, I want to click on the button in the parent frame of the page. - Attempting to switch back to the parent frame ...

"Encountered an issue while attempting to run the docker-compose script on AWS

Launching my application with sudo docker-compose up works fine, but when I try docker-compose up, it throws the following error: $ docker-compose up Traceback (most recent call last): File "urllib3/connectionpool.py", line 670, in urlopen Fi ...

Django's Secure User Authentication and Account Access

I need assistance creating a login function and authentication in the views.py file within def login(request). I am looking to authenticate a specific username and password from my model class "Signup". If anyone can provide guidance on how to accomplish t ...

The latest version of Django Report Builder (3.1.12) seems to be having trouble displaying fields for certain models

Currently, I am in the process of setting up django-reportbuilder for a new website. However, I have encountered an issue where the reports are not displaying any fields for some of my models. When I try to click on them, the field list remains empty and I ...

In Python, let's create a drawing of a face expressing both anger and

I attempted to modify the following code in order to create an angry and surprised facial expression, but I was unsuccessful. Can anyone provide assistance? import turtle face = turtle.Turtle() face.penup() face.goto(-75,150) face.pendown() face.circle ...

Using the open file dialog feature within a Java Selenium test case

I'm currently working on an angular application and writing test cases using selenium. I encountered a scenario within one of my test suites where I need to trigger the opening of a Windows FileInput dialog by clicking a button, then selecting a file ...

In both Python 2.x and 3.x, is range subject to eager or lazy evaluation?

Upon researching the range function, I discovered that (EDIT: in 2.x) it is eagerly evaluated >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] However, running the following code in my local Python 3.3 interpreter gives me different results: a = ra ...

group and apply operations to all remaining keys

If I have a pandas dataframe called df, I can find the average reading ability for each age by using the code df.groupby('Age').apply(lambda x: x['ReadingAbility'].mean()). But what if I want to find the average reading ability for all ...

Extracting multiple elements from XPath using Selenium in Python

After creating this XPath expression, I am encountering an issue where only the first element is being retrieved. However, there are actually 3 or 4 elements with the same XPath that I need to capture. The number of elements can vary from page to page, ra ...

Is there a way to modify a JSON dictionary at different levels?

Can you assist me with updating the value of a dictionary key in a JSON file using variable keys? Here's what currently works and the problem I'm encountering. For example: print(my_json['convergedSystem']['endpoints'][0][& ...

What is the correct way to submit a form object on a website?

Currently, I am practicing CBV and wanted to test if I can override methods. One major issue I encountered is that I am unsure how to utilize recently submitted data. In an attempt to address this, I have crafted the following code for a DetailView, which ...

Combine specific data from a pandas dataframe

I am working with a dataframe that has the following structure: ID SCORE DATE 0 123 98.5 20210520 1 123 97.3 20210518 2 123 95.7 20210430 3 456 87.6 20210312 4 789 92.1 20210228 ...

What is the process for locating elements with the listitem role using Selenium?

Having trouble accessing and cycling through a collection of list items on a webpage using Python. The elements to be fetched are defined as follows, with 4 present on the page: <div data-test-component="StencilReactView" role="listitem& ...

Using WebDriver in C# to select an option from a dropdown menu based on its

Struggling to choose a specific word from a dropdown menu. When using Webdriver IDE, it shows the steps to click on the dropdown (which has an ID of "p") and then select the word "Barcelona" from within the dropdown: https://i.stack.imgur.com/rAhKH.jpg ...

Accessing a variable that is scoped within a function

The following code snippet showcases a common error in Python programming: def reportRealDiagnostics(): ranks = 0 class Rank: def __init__(self): global ranks ranks += 1 rank = Rank() ...