python - Python Selenium Loop 点击链接
问题描述
我无法循环单击链接。当我尝试循环单击链接时,它只会单击第一个链接。
从 html 代码中,我还需要名为“key”值的元素。如何捕捉它。
将 html 文件复制到 Dropbox 中。请点击https://www.dropbox.com/sh/85rx13m8iqwax4b/AACNDq_YyOukLh22JNv76vjua?dl=0
。
html代码
https://pastebin.com/Cyg98W2C
我试过的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()
编辑:添加了保管箱链接。由于该网站仅登录。我已经制作了该页面的副本。
解决方案
您可以收集所有元素,然后使用相对查找来查找您需要的链接。请注意,如果您不在新窗口中打开点击,这可能会导致元素过时。
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
推荐阅读
- android - Android 使用安全参数对 java.time.LocalDate 进行脱糖
- azure-policy - 创建资源组时用于创建标记的 Azure 策略不起作用
- python - 您可以使用单个列表推导返回给出某个函数的最小值/最大值的列表元素吗?
- laravel - 使用 postmant 发布 laravel sanctum
- huawei-mobile-services - 华为 P30 Pro 上的系统日志
- python - 在 python 中读取 .dat 文件(Agilent 4294A 精密阻抗分析仪)
- android - 使用 Android Intent (Java) 将视频分享到 WhatsApp 和其他应用程序
- winforms - 如何使用 WebView2 控件动态更改 .NET Framework 4.8 C# WinForm 桌面应用程序的不透明度
- java - Java 包中“导入”的范围
- reactjs - 类型“HTMLIonListElement”上不存在属性“显示”