Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98749

Not able to exit the while loop after the click is not available

$
0
0

What I am trying to achieve is that I have a list of similar elements present in a page from which I want to extract the attribute from the element. While doing so once one page is done the code should click the next button until till the last page and perform the similar operation of extraction once done move to the next link in the for loop can anyone help me in achieving this, following is the code that I am using what it does is it clicks the next button but never exits the while loop.

for link in content[currentIndex:]:
    now = datetime.now()
    # dd/mm/YY H:M:S
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    print("date and time =", dt_string)
    driver.get(link)
    time.sleep(3)
    try:
        while driver.find_element_by_css_selector('.a-last').click():
            try:
                while driver.find_element_by_css_selector('.a-disabled a-last.a-last'):
                    print("nothing here")
                    break
            except NoSuchElementException:
                aa = driver.find_elements_by_css_selector(
                    '.sg-col-4-of-24.sg-col-4-of-12.sg-col-4-of-36.s-result-item.sg-col-4-of-28.sg-col-4-of-16.sg-col.sg-col-4-of-20.sg-col-4-of-32')
                for j in aa:
                    hd = j.get_attribute('data-asin')
                    hd1 = "'" + hd
                    print(hd1)
                    f.write(link + "," + hd1 + "\n")
                driver.find_element_by_css_selector('.a-last').click()
    except NoSuchElementException:
        pass

The link that I am using is "https://www.amazon.com/s/ref=sr_in_h_p_89_87?fst=as%3Aoff&rh=i%3Atoys-and-games-intl-ship%2Cn%3A%2116225015011%2Cn%3A276729011%2Cp_89%3AHobby+Products+International&bbn=16225015011&ie=UTF8&qid=1578465210&rnid=2528832011" in this i want to extract attribute "data-asin" for all the elements.

can any one help seriously i am stuck at this


Viewing all articles
Browse latest Browse all 98749

Trending Articles