Selenium is only able to retrieve a single result at a time, disregarding any other connected outcomes

Recently delving into the world of selenium, I encountered an issue while trying to scrape a website with 10 results per page displayed in lists (li tags). Each list contains the same attributes and when certain conditions are met, I navigate to another related page to extract desired content. However, my code fails to find the same attributes for subsequent lists after looping through the initial ones. Below is the snippet of my code:

p_url = "https://www.linkedin.com/vsearch/f?keywords=BARCO%2BNV%2Bkortrijk&pt=people&page_num=5"             
driver.get(p_url)

time.sleep(5)

results = driver.find_element_by_id("results-container")
employees = results.find_elements_by_tag_name('li')

for emp in employees:
    try:

        main_emp = emp.find_element_by_css_selector("a.title.main-headline")
        name = emp.find_element_by_css_selector("a.title.main-headline").text
        href = main_emp.get_attribute("href")

        if name != "LinkedIn Member":
            location = emp.find_element_by_class_name("demographic").text
            href = main_emp.get_attribute("href")
            print(href)
            print(location)

            driver.get(href)
            exp = driver.find_element_by_id("background-experience")

            amkk = exp.find_elements_by_class_name("editable-item")

            for amk in amkk:
                him = amk.find_element_by_tag_name("header").text
                him2 = amk.find_element_by_class_name("experience-date-locale").text

                if '\n' in him:
                    a = him.split('\n')
                    print(a[0])
                    print(a[1])

                print(him2)

    except Exception as exc:
        print(exc)
        continue

The line

main_emp = emp.find_element_by_css_selector("a.title.main-headline")
in this code stops functioning after the first iteration, resulting in a
Message: stale element reference: element is not attached to the page document
error.

After researching on stackoverflow, some users suggest that the content might be removed from the DOM structure, while others recommend storing the results in a list. I attempted to create a list of elements like so

emp_list = []
for i in range(len(employees)):
    emp_list.append(employees[i])
, but it did not resolve the issue.

What steps can I take to overcome this obstacle?

Answer №1

You are not using the correct selector in your code. While you are able to grab results using the results-container id, the issue arises when collecting elements from it. This is resulting in more elements being retrieved than just the employees (the reason for this is unclear).

To fix this issue, try using a single selector that specifically targets only the employees without any unwanted elements.

employees = results.find_elements_by_css_selector("ol[id='results']>li")

Edit: If you find yourself navigating through the employees and then losing track of the list of elements, consider opening each employee in a new tab, performing actions there, and then closing the tab afterwards.

Example:

for emp in employees:
    try:
        main_emp = emp.find_element_by_css_selector("a.title.main-headline")
        # Do necessary actions...

        # Open employee in a new tab (make sure Keys is imported)
        main_emp.send_keys(Keys.CONTROL + 't')
        
        # Switch focus to the new tab
        driver.switch_to_window(d.window_handles[1])

        # Perform actions within the employee page
        
        # Close the opened tab
        driver.close()
        
        # Return to the original tab
        driver.switch_to_window(d.window_handles[0])

Note: For MacOS, use

main_emp.send_keys(Keys.COMMAND + 't')

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

Guide on verifying the clickability of a button with selenium webdriver

I am currently working on determining whether or not the button element is clickable, however I am facing difficulties in successfully validating this using Selenium WebDriver. Below is the code snippet I am using to validate if the element is clickable: ...

Why is it that I'm encountering this basic mistake in Selenium?

I am currently using an AWS micro instance without a graphical user interface. I have accessed it through SSH. pip install selenium sudo apt-get install firefox Next, I executed the following in the Python shell: >>> from selenium.webdriver.fir ...

Creating Selenium reports with Jenkins (formerly known as Hudson) from JUnit XML files

When it comes to automating tests for our web project, we rely on a combination of Hudson, PHPUnit, and Selenium. Our test results are saved in the JUnit XML format. However, when attempting to include report generation using the Hudson feature Publish JU ...

Utilize Selenium C# to locate and update information within a text file

I have a situation where I need to upload a text file in the UI, but this text file requires updating every time before the test is run. To achieve this functionality, I am looking to capture it within my Selenium C# script. The data format of the text fi ...

What is the best method to override a class attribute that is two levels deep?

When it comes to overriding and inheritance, I've noticed that things are not working as expected in my code. Let me break down the scenario for you: I have three classes - Card, Treasure, and Copper. Copper inherits from Treasure, and Treasure inheri ...

Leveraging Python for populating a text field on a website using an If Statement

Hi there, I am new to Python and looking for some assistance. My goal is to automate text input on a website. Specifically, I want the code to check if an input box is empty - if it is, then type 4.00, otherwise press the down key. I have included an image ...

Guide for setting the executable path of the Chrome driver to match the operating system. My goal is to create a generic executable path for the Selenium driver using Python

What is the best way to define the executable path of the Chrome driver to match the operating system's path? I am aiming to create a universal executable path that works across all systems for the Selenium with Python driver. ...

In relation to the List function within the Python programming language

I'm encountering an issue with creating lists in Python for a specific purpose. I've devised this code to illustrate my problem: I aim to establish a list of names and utilize it as a reference when logging in to prevent "Access Denied" errors. D ...

Tips for utilizing the "next_in_line" attribute in place of "super"

This question focuses on how to approach a certain task rather than questioning whether it should be done. I am aware of the limitations that come with such a change. In an insightful presentation titled Super considered super!, Raymond Hettinger expresse ...

Tips on enabling direct downloads for various file formats (bzip2 and csv) using RSelenium

Below is a code snippet that enables direct download specifically for bzip2 files: require(RSelenium) dirdownload <- "/path_to/my_output_dir" fprof <- makeFirefoxProfile(list(browser.download.dir = dirdownload, browse ...

Getting the text of an element in Selenium Webdriver: A step-by-step guide

When attempting to retrieve the content inside a textbox using the ID, I am encountering an issue. Despite using the gettext() method, it is returning the ID value instead of the actual content. The text box currently contains: Santhosh The output I rece ...

Selenium is unable to function properly with a chromedriver that has been altered to evade detection

My question arises from a specific issue I encountered while using Selenium for web scraping. As seen in this thread and this thread, the suggested solution to modify the ChromeDriver no longer seems to work effectively. Despite the advice provided in an o ...

Having trouble utilizing Webdriver Manager on my Raspberry Pi, encountering the issue "Unable to retrieve Firefox version using this command: Firefox --version"

I am currently running a Python script on my Raspberry Pi that opens up a Firefox tab. Here is my code: from selenium import webdriver from pyvirtualdisplay import Display from webdriver_manager.firefox import GeckoDriverManager display = Display(visible ...

Selenium test is interrupted by a Firefox pop-up blocking part of the screen

After running my test on BrowserStack, I noticed an interesting outcome. Is there a way to disable this particular behavior of Firefox within a selenium test using DesiredCapabilities? I prefer being able to control this behavior through settings rather ...

The IgnoreExceptionTypes feature in Selenium C# DefaultWait does not function as intended

When I utilize DefaultWait to wait for a WebElement to be clickable, I have included TargetInvocationException in the list of exceptions to be ignored during the waiting process. However, some tests are still failing with this exception before the Timeou ...

Use Node.js with Selenium and WebdriverIO to simulate the ENTER keypress action on

I have put in a lot of effort trying to find the solution before resorting to asking this question, but unfortunately I have not been successful. All I need to know is how to send special characters (such as the enter key and backspace) with Node.js using ...

What is the best way to ensure that an Odoo computed relation field automatically calculates itself during the import process

In a certain view, four fields have been added: start, end, ignore, and range. The range field is calculated based on the values of start and end, with the option to use ignore to exclude specific records. While the method works well under normal circums ...

Using JavaScript in Selenium, you can check a checkbox and verify whether it is checked

How can I select a checkbox and determine if it is checked using JavaScript, Selenium, and Node.js? I have attempted to use the .click method on the element and also tried native JavaScript code, but unfortunately it has not worked for me. driver.findElem ...

The way Xpath is used to locate the same checkbox element can vary between Chrome and Mozilla browsers when working with Selenium in Python

There is a different xpath for the checkbox in Chrome (Version 81.0.4044.138) and Firefox (76.0.1 (64-bit)). The checkbox works for one browser at a time, but I am encountering the following error: ============================= test session starts ======= ...

Converting the output to JSON format for archival purposes

Having trouble writing my output to a JSON file in the correct format. The code og = OpenGraph(i, ["og:title", "og:description", "og:image", "og:url"]) is not producing valid JSON. Can someone point out what I'm doing wrong? # -*- coding: utf-8 -*- i ...