Python Selenium Loop click through links

谁说我不能喝 提交于 2021-01-29 08:07:09

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/64865614/python-selenium-loop-click-through-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!