0

I'm trying to click multiple links from a page but it seems that I can only click the first link and then the program crashes at the second link. This is the program that I'm running:

    driver = webdriver.Chrome("C://Users//user/chromedriver.exe")

    profile_url = "https://www.goodreads.com/list?ref=nav_brws_lists"
    driver.get(profile_url)
    src = driver.page_source

    soup = bs(src, "lxml")
    ul_wrapper = soup.find_all("ul", {"class": "listTagsTwoColumn"})

    for tag in ul_wrapper:
        list = tag.find_all("li")
        for li in list:
            a = li.find("a")
            elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
                (By.XPATH, "//a[@href='" + a.get("href") + "']")))
            elem.click()
            driver.back()

This is the error that I get on the elem.click() function: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <a class="actionLinkLite" href="/list/tag/...">fiction</a> is not clickable at point (704, 677). Other element would receive the click: <div class="modal modal--overlay modal--centered" tabindex="0" data-reactid=".1i6bv62jphg">...</div> (Session info: chrome=108.0.5359.125)

The program works as expected for the first link. It clicks through it and I'm able to get the contents. But it fails for the second loop. I'm expecting to be able to click through all the links. Any suggestions?

  • Does clicking on the first link open a new tab? Moreover, notice that usually if you store some webelements and then load a new page, then the reference to those elements are lost even if you have stored them in a variable – sound wave Jan 04 '23 at 14:19
  • It doesn't open a new tab. I check the driver page title and it shows the correct next page after first click. How should I handle the reference to the web elements that I'm referencing in order to make it work? – Paul Buciuman Jan 04 '23 at 14:33
  • Ah but there should not be any problem since you are redefining `a` and `elem` at each loop, so try to add `time.sleep(10)` after `driver.back()` – sound wave Jan 04 '23 at 15:07

1 Answers1

0

You could try to use javascript to click the element. See below.

driver = webdriver.Chrome("C://Users//user/chromedriver.exe")

profile_url = "https://www.goodreads.com/list?ref=nav_brws_lists"
driver.get(profile_url)
src = driver.page_source

soup = bs(src, "lxml")
ul_wrapper = soup.find_all("ul", {"class": "listTagsTwoColumn"})

for tag in ul_wrapper:
    list = tag.find_all("li")
    for li in list:
        a = li.find("a")
        elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
            (By.XPATH, "//a[@href='" + a.get("href") + "']")))
        # elem.click()
        driver.execute_script("arguments[0].click();", elem)
        driver.back()
Jortega
  • 3,616
  • 1
  • 18
  • 21