简体   繁体   中英

Python-Selenium Fetching data from previous listing webpage using selenium in python

I have a scenario I am trying to fetch some data from detail page using selenium and python. I am new to selenium and Python

I tried to use self.driver.execute_script("window.history.go(-1)")

to go back to previous page and start fetching 2,3, 4 record etc but issue is: After fetching 1 record, Click event occur it moves to details page and fetch remaining data But, when it move backward to the listing page from detail page it throws error

On cmpname = selectAll.find_element_by_css_selector(".Capsuletitle h2")

It throws error StaleElementRefrenceException: Element is not attached to the page document

Basically, What I wanted I have listing page and detail page for each record I want to fetch data from both pages

Here is my loop code part

parentTab = self.driver.find_element_by_class_name("capsuleList")
    for selectAll in parentTab.find_elements_by_class_name("bsCapsule"):

        cmpname = selectAll.find_element_by_css_selector(".Capsuletitle h2")
        print(cmpname.text)

        address = selectAll.find_element_by_css_selector(".Capsuleaddress a span")
        print(address.text)

        telephone = selectAll.find_element_by_css_selector(".Capsuletel")
        print(telephone.text)

        selectAll.find_element_by_css_selector('.Capsuletitle div a').click()
        time.sleep(20)
        adrurl = self.driver.find_element_by_css_selector('.CapsulecallToAction a').get_attribute('href')
        print(adrurl)
        self.driver.execute_script("window.history.go(-1)")
        time.sleep(20)

Regards

First, instead of using self.driver.execute_script("window.history.go(-1)"), you can just use driver.back(). As for the error your getting, the most frequent cause of this is that page that the element was part of has been refreshed, or the user has navigated away to another page. So I suggest you try looking up the element again after you go back a page. I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM