0

I am unable to loop click the links. When I try loop click the links it keeps clicking the first link only.

From the html code, I need the element named "key" value as well. How to capture it.

html file copy in dropbox. Please click https://www.dropbox.com/sh/85rx13m8iqwax4b/AACNDq_YyOukLh22JNv76vjua?dl=0.

html code

https://pastebin.com/Cyg98W2C

Python code I tried

elem = WebDriverWait(browser, 200).until(EC.element_to_be_clickable((By.XPATH, "//DIV[@id='propertySummaryList']/DIV[@class='summaryListItem   ']/DIV[1]/DIV[3]/DIV[1]/H2[1]/A[1]")))
     elem.click()
     browser.back()

Edit: Added dropbox link. Since the site is sign in only. I have made a copy of the page.

ACE
  • 45
  • 7

1 Answers1

0

You can gather all the elements, then use a relative find to find the link you need. Be careful, this may cause stale elements if you don't open the click in a new window.

summaryList = driver.find_elements_by_xpath("//DIV[@id='propertySummaryList']/DIV[@class='summaryListItem   ']")
for elements in summaryList:
    link = elements.find_elements_by_xpath(".//h2//a")
    link.text // or link.click() but need to open in a new window or will get staleElementReference
DMart
  • 2,401
  • 1
  • 14
  • 19
  • I have ran the script as per your example. But got error like ```python AttributeError: 'list' object has no attribute 'click'``` – ACE Nov 17 '20 at 00:31
  • That's probably because `find_elements_by_xpath` generates a list. Try using `link = elements.find_element_by_xpath(".//h2//a")` instead. – Patrick Klein Nov 17 '20 at 14:48
  • I guess I am on the right track. As you said I have received staleElement error. I am figuring out how to open links in new tab or window. – ACE Nov 17 '20 at 14:57
  • @PatrickKlein I have added dropbox link with html files. Please take a look. – ACE Nov 17 '20 at 15:03
  • You can try this to open links in new window: https://stackoverflow.com/a/19152396/1387701 – DMart Nov 17 '20 at 15:35
  • I tried but getting error. `action = browser.action AttributeError: 'WebDriver' object has no attribute 'action'` – ACE Nov 17 '20 at 17:52
  • Even when I try to manually CTRL + CLICK doesn't open the links just a blank window, I believe it is due to javascript links. – ACE Nov 17 '20 at 18:42