我正在使用Python 3.7.2,并通过PIP安装了Selenium。我在Windows 10 1809计算机上。我正在使用chromedriver。我可以使用文档中的示例代码。到目前为止,我已经可以打开网站并进行搜索了,但是(对我来说)我一直处于障碍之下,而且我不知道如何遍历要打开的链接列表。
以下代码用于“单个列表”项目。当我单击标题时,它会打开页面。
<a class="clickable" style="cursor:pointer;" onmousedown="open_listing_detail('abcd321', '0', false)" title="View Listing Detail" key="abcd321">Some xyz Listing </a>
每页大约有20个项目。我需要在NewTabs中打开这20个项目。
否则,我想打开链接->从页面中获取信息->关闭页面->打开第二个链接->重复
要移至下一页,我需要单击下一步按钮。以下是该元素的代码。
<li key="2" class="pageLink page">Next</li>
我是编程/脚本新手。让我知道您是否需要其他详细信息来澄清我的查询。
答案 0 :(得分:0)
好吧,如果我能正确理解您的查询,这就是解决方案。
# iterate while next button is enabled
while (driver.find_element_by_xpath("//li[@class='pageLink page' and .='Next']").is_enabled()):
# get all the links in the current page
links = driver.find_elements_by_xpath("//a[@class='clickable' and contains(@onmousedown,'open_listing_detail')]")
baseURL = driver.current_url
# open the link and perform operation in new tab
for link in links:
# click on link
link.click()
print(driver.title)
print(driver.current_url)
#close the tab
# click on back
driver.back()
# alternatively you can use the url
#driver.get(baseURL)
driver.find_element_by_xpath("//li[@class='pageLink page' and .='Next'].click()