简体   繁体   English

Python Selenium Loop 点击链接

[英]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.从 html 代码中,我还需要名为“key”值的元素。 How to capture it.如何捕捉它。

html file copy in dropbox.在 Dropbox 中复制 html 文件。 Please click https://www.dropbox.com/sh/85rx13m8iqwax4b/AACNDq_YyOukLh22JNv76vjua?dl=0 .请点击https://www.dropbox.com/sh/85rx13m8iqwax4b/AACNDq_YyOukLh22JNv76vjua?dl=0

html code html代码

https://pastebin.com/Cyg98W2C https://pastebin.com/Cyg98W2C

Python code I tried我试过的 Python 代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM