0

I have an array of links that I am trying to access to every link and print something from it, then return to the main page and access the second link, then do the same until I finish all links in the array.

What happens is that the first link is the only one that works, like if all the links in the array are gone. I get the error:

File "e:\work\MY CODE\scraping\learn.py", line 25, in theprint link.click()

    from selenium import webdriver
from selenium.webdriver.common import keys
#it make us able to use keybored keys like enter ,esc , etc....
from selenium.webdriver.common.keys import Keys
import time

#make us can wait for event to happen until run the next line of code
from selenium.webdriver.common.by import By
from selenium.webdriver.remote import command
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#get the google chrome driver path
PATH="E:\work\crom\chromedriver.exe"
#pass the pass to selenium webdriver method
driver=webdriver.Chrome(PATH)
#get the link of the site we want
driver.get("https://app.dealroom.co/companies.startups/f/client_focus/anyof_business/company_status/not_closed/company_type/not_government%20nonprofit/employees/anyof_2-10_11-50_51-200/has_website_url/anyof_yes/slug_locations/anyof_france?sort=-revenue")

#wait for the page to load
time.sleep(5)
#get the links i want to get info from
the_links=driver.find_elements_by_class_name("table-list-item")

#function that go the link and print somethin and return to main page
links=[]
the_links=driver.find_elements_by_class_name("table-list-item")
for link in the_links:
      links.append(link.get_attribute('href'))

for link in links:
      driver.get(link)
      website=driver.find_element_by_class_name("item-details-info__url")
      print(website.text)
      driver.back()
      time.sleep(3)
      
2
  • Are you getting a stale element reference? You can't define an element, switch pages, then use that element again. It looks like that's what you are trying to do, which would cause a stale element error.' Commented Jun 2, 2021 at 23:37
  • yes, I got that can you tell me another way to do it ?? Commented Jun 3, 2021 at 5:26

2 Answers 2

1

Your code will throw a stale element reference error because when you navigate to the next page, the variable holding any elements of the previous page will become unusable.

So what you need to do is either store all elements in array and then loop through it like this:

links=[]
the_links=driver.find_elements_by_class_name("table-list-item")
for link in the_links:
    links.append(link.get_attribute('href'))

for link in links:
    driver.get(link)
    print("do something on this link")

Or you can use a while loop in your current and after driver.back() again populate the the_links variable.

Sign up to request clarification or add additional context in comments.

2 Comments

okay, I tried this code but the get() method doesn't work do you know why? that what I added to it : for link in links: driver.get(link) website=driver.find_element_by_class_name("item-details-info__url") print(website.text) driver.back() time.sleep(3)
If get method not working then you can use JavaScript executor to load new URL. For example, driver.execute_script("window.location.href = {}".format(link)). This will take you to new url.
0

Karim, is the element with class_name "item-details-info__url" present on all pages? Also, what error does get() method throwing?

1 Comment

yes, the class is present on all pages, the get() method simply doesn't work at all and I get an error in the line it exist in I edited the code now you can see it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.