Unable to interact with element on the following page using Selenium in Python

Attempting to extract data from multiple pages of a URL using Selenium in Python, but encountering an error after the first page. The code successfully navigates to the subsequent pages but fails to scrape, throwing an error message: "Element ... is not clickable at point (208, 17). Other element would receive the click: ...". Below is the snippet of the code:

import pandas as pd
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E

# Function to scrape through pages
def page_scrape():
    driver.maximize_window()
    ADDRESS_LOCATIONS_TEASER = '//div[contains(@class,"all-stores accordian ng-star-inserted")]'
    ADDRESS_LOCATIONS = './/div[contains(@class,"accordian-header")]'
    teaser = driver.find_element(By.XPATH, ADDRESS_LOCATIONS_TEASER)
    locations = teaser.find_elements(By.XPATH, ADDRESS_LOCATIONS)

    for loc in locations:
        add = loc.find_element(By.XPATH, './/a[@href]').click()
        add1 = driver.find_element(By.XPATH, ".//address[contains(@class, 'address-block')]").text
        print(add1)
        
        # More scraping logic here ...

# Instantiate Chrome WebDriver
driver = webdriver.Chrome("C:/Users/doyel/Downloads/chromedriver_win32/chromedriver.exe")
driver.get('https://www.kfc.de/find-a-kfc')

results = pd.DataFrame(columns=['address', 'PLZ', 'Telephone' 'Restaurant Services'])

COOKIE_PATH = '//button[contains(@id,"onetrust-accept-btn-handler")]'
driver.find_element(By.XPATH,COOKIE_PATH).click()

while True:
    page_scrape()
    next_page = '//a[@aria-label="Next page"]'
    try:
        driver.find_element(By.XPATH, next_page).click()
        print("next page")
        time.sleep(2)

    except:
        print("last page reached")
        break
        
driver.quit()

Answer №1

Although it may not be easy, it is definitely achievable, and here's a method to tackle it:

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
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time as t 
from tqdm import tqdm ## if using Jupyter notebook, import as from tqdm.notebook import tqdm

pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1920,1080")

webdriver_service = Service("chromedriver/chromedriver") ## provide the path where you saved chromedriver binary
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(driver, 5)

restaurant_list = []
driver.get('https://www.kfc.de/find-a-kfc')

try:
    wait.until(EC.element_to_be_clickable((By.ID, "onetrust-reject-all-handler"))).click()
    print('cookies dismissed')
except Exception as e:
    print('cookie button unavailable!')
header = wait.until(EC.element_to_be_clickable((By.TAG_NAME, "app-common-header")))
driver.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", header)
for x in tqdm(range(1, 21)):
    kfc_rests = wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div[@class="all-stores accordian ng-star-inserted"]//app-accordian[@class="card carryout-address ng-star-inserted"]')))
    for k in kfc_rests:
        k.location_once_scrolled_into_view
        k.click()
        name = k.find_element(By.TAG_NAME, 'strong').text
        address = wait.until(EC.element_to_be_clickable((By.XPATH, '//address[@class="address-block"]/p'))).text.replace('\n', ' ').strip()
        try:
            services = ', '.join([x.text.strip() for x in wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div[@class="services ng-star-inserted"]//li')))])
        except Exception as e:
            services = 'Not specified'
        restaurant_list.append((name, address, services))
    try:
        next_page = wait.until(EC.element_to_be_clickable((By.XPATH, '//li[@class="pagination-next ng-star-inserted"]//a[@aria-label="Next page"]')))
        next_page.location_once_scrolled_into_view
        next_page.click()
    except Exception as e:
        print('end of list')
        break
df = pd.DataFrame(restaurant_list, columns = ['Name', 'Address', 'Services'])
print(df)

The output in the terminal:

cookies dismissed
95%
19/20 [01:41<00:05, 5.91s/it]
end of list
Name    Address Services
0   KFC BERLIN  Grenzallee 37 12057 Berlin  Delivery, Drive Thru, Free Refill, Credit Card Payment, Click & Collect
1   KFC BERLIN  Gatower Straße 56 13595 Berlin  Delivery, Drive Thru, Free Refill, Credit Card Payment, Click & Collect
2   KFC BERLIN  Mall of Berlin Leipziger Platz 12 10117 Berlin  Delivery, Free Refill, Credit Card Payment, Click & Collect
3   KFC BERLIN  Klosterstraße 3 13581 Berlin    Delivery, Credit Card Payment, Click & Collect
4   KFC BERLIN  Schnellerstr. 18a 12439 Berlin  Drive Thru, Free Refill, Credit Card Payment, Click & Collect
... ... ... ...
191 KFC SAARBRÜCKEN Wolfseck 6 66130 Saarbrücken    Drive Thru, Free Refill, Credit Card Payment
192 KFC SAARLOUIS   Provinzialstr. 246 66740 Saarlouis  Drive Thru, Free Refill, Credit Card Payment
193 KFC OFFENBURG   Heinrich-Hertz-Str. 3 77656 Offenburg   Drive Thru, Free Refill, Credit Card Payment
194 KFC FREIBURG    Tullastraße 68 79108 Freiburg   Delivery, Drive Thru, Free Refill, Credit Card Payment
195 KFC FRANKFURT AIRPORT Tullastraße 68 79108 Freiburg   Delivery, Drive Thru, Free Refill, Credit Card Payment
196 rows × 3 columns

To explore Selenium further, visit: https://www.selenium.dev/documentation/

For Pandas documentation: https://pandas.pydata.org/docs/

And for TQDM usage guide, head to https://pypi.org/project/tqdm/

Answer №2

I have found a straightforward solution using (VBA) that has proven to be effective in various scenarios (feel free to adapt the code to Python):

driver.ExecuteScript "arguments[0].click();", driver.FindElementByXPath("//div[@id='search-engine']/div/div/div[2]") 

Feel free to utilize findelementbywhatever as needed...

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

I am encountering difficulties in installing the TensorFlow library in Python

My computer runs on a 64-bit system and I have Python version 3.7.9 installed. I'm facing an issue with installing TensorFlow. Can anyone provide assistance on this? Please let me know if more information is required. https://i.stack.imgur.com/L1tzz.p ...

There was an issue parsing the parameter '--request-items' due to invalid JSON format. Decoding failed as no JSON object could be found

My current environment includes AWS cloud, DynamoDB, AWS Cloud9, Python, and JSON. I'm attempting to write these elements into a DynamoDB table using the command aws dynamodb batch-write-item --request-items file://Sensors.json in my Cloud9 CLI. How ...

Adjusting the parameters of my support vector machine (SVM) classifier trained on a sample of the MNIST digit dataset does not seem to have any impact on the accuracy of

Being a novice in the realm of machine learning, I am currently delving into the realm of hyperparameters for an SVM with the goal of achieving at least 95% accuracy on the mnist digit dataset. The csv file being utilized by the code contains 784 attribut ...

The PyJWT library throws a Signature verification failed error when attempting to decode a JWT

I encountered an unusual problem when trying to decode the jwt token in my Django views. Whenever I use jwt.decode('encoded_token', 'secret'), I receive the "Signature verification failed" message. To work around this issue, I've t ...

I require assistance with figuring out how to trigger a scroll up event

I've been struggling to find a solution in Python using Pygame that allows me to detect scrolling up. My goal is to recreate the experience from the popular video "Interstellar Mouse" https://www.youtube.com/watch?v=aANF2OOVX40 I want to trigger the ...

Retrieve all href links using the Python selenium module

Exploring Selenium with Python has been my recent interest, and I was eager to extract all the links present on a particular web page using Selenium. Specifically, I wanted to retrieve all the links specified in the href= attribute of every <a> tag ...

Enable access for a GAE application to a Google API using the google-api-python-client library

Currently, I am in the process of developing a Google App Engine application where users can fill out an online form. Depending on how the form is filled out, a calendar event is created in a specific Google Calendar. My concern lies with the authorization ...

What is the process for extracting the download button URL and parsing a CSV file in Python?

In my Python Google Colab project, I am attempting to access a CSV file from the following link: After scrolling down slightly on the page, there is a download button visible. My goal is to extract the link using Selenium or BeautifulSoup in order to read ...

Guidelines for incorporating my company's PPT slide designs in your python-pptx presentation

When I run the following code: from pptx import Presentation prs = Presentation() slide_layout = prs.slide_layouts[0] prs.save("Final.pptx") A PowerPoint presentation is generated with a single slide that uses the default title layout. However, when I o ...

Using Selenium to interact with a link's href attribute through JavaScript

New to Java and Selenium, I'm facing difficulties when trying to click on a link with JavaScript in href attribute. Here's the snippet from the page source: href="javascript:navigateToDiffTab('https://site_url/medications','Are y ...

Python script for extracting data from live YouTube chats

I am attempting to extract chat messages from YouTube live chat. Initially, I tried a method outlined in "https://www.youtube.com/watch?v=W2DS6wT6_48" Unfortunately, the code did not function as expected. An error message was generated: all_comments = d ...

Selenium in Python struggling to locate dynamically loaded JavaScript table following automated login process

I am currently utilizing Selenium with Python3 to navigate a Service Now Website. The process is as follows: Selenium loads the ServiceNow URL and I use sendKeys to automate entering the username and password. Once the page is loaded, there is a table of ...

Can anyone give me advice on how to enable automatic suggestions for array options while I'm typing in Vim?

If I were to input the following code in a .py file using vim: a = [1, 2] and then type "a." and press TAB, I am expecting to see a suggestion menu specifically related to lists. Edit 1: In response to Robin's comment: I believe achieving this func ...

I am struggling to comprehend the einsum calculation

As I attempt to migrate code from Python to R, I must admit that my knowledge of Python is far less compared to R. I am facing a challenge while trying to understand a complex einsum command in order to translate it. Referring back to an answer in a previ ...

Retrieving a JavaScript variable from a Python Selenium request

Currently, I am utilizing Python in conjunction with Selenium to monitor changes on our pbx system. A specific value that I require is being retrieved through a JavaScript call and unfortunately, it is not directly written into the HTML code. This has made ...

What is the best way to create a function that can accept a variable number of arguments?

I am looking to create a function that is able to, for instance, add up all given parameters: def sum(#elements): return(a+...#all elements) print(sum(1,3,4)) ...

Can anyone provide guidance on how to navigate and locate this specific element using Selenium within a Java

How do I locate the value 1352 using Java Selenium on ChromeDriver? <span class="numfound" id="yui_3_18_1_1_1522936314968_15">1352</span> The id is not intuitive, so I want to select by the String "numfound". I attempted selecting byClassName ...

Is there a way to effortlessly include the root directory to the python path in VSCode?

I recently made the switch from PyCharm to VSCode and I am encountering an issue with importing modules within the same package. main.py from importlib_resources import files import household_prices.data.raw_data as raw_data # <- module not found sou ...

Ways to eliminate attributes from dataclasses

I am working with a dataclass that looks like this: @dataclass class Bla: arg1: Optional[int] = None arg2: Optional[str] = None arg3: Optional[Dict[str, str]] = None My objective is to achieve the following behavior: >>> bla = Bla(a ...

Selenium in C#: Timeout issue with SendKeys and Error thrown by JS Executor

Attempting to insert the large amount of data into the "Textarea1" control, I have tried two different methods. The first method successfully inserts the data but occasionally throws a timeout error, while the second method results in a JavaScript error. A ...