简体   繁体   中英

Python Selenium Loop click through links

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.

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

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