【发布时间】:2021-07-26 01:08:38
【问题描述】:
我的代码进入一个网站,点击导致下拉的记录。
我当前的代码只打印第一个下拉记录,而不打印其他记录。
例如,网页的第一条记录被点击时,下拉1条记录。该记录显示为附件。这也是第一个也是唯一一个作为我的输出打印的下拉记录。
代码打印这个
如何让它拉出所有下拉标题?
from selenium import webdriver
import time
driver = webdriver.Chrome()
for x in range (1,2):
driver.get(f'https://library.iaslc.org/conference-program?product_id=24&author=&category=&date=&session_type=&session=&presentation=&keyword=&available=&cme=&page={x}')
time.sleep(4)
productlist_length = len(driver.find_elements_by_xpath("//div[@class='accordin_title']"))
for i in range(1, productlist_length + 1):
product = driver.find_element_by_xpath("(//div[@class='accordin_title'])[" + str(i) + "]")
title = product.find_element_by_xpath('.//h4').text.strip()
print(title)
buttonToClick = product.find_element_by_xpath('.//div[@class="sign"]')
buttonToClick.click()
time.sleep(5)
subProduct=driver.find_element_by_xpath(".//li[@class='sub_accordin_presentation']")
otherTitle=subProduct.find_element_by_xpath('.//h4').text.strip()
print(otherTitle)
【问题讨论】:
-
你明白
range(1,2)只产生一个元素“1”吗?如果你想要数字 1 和 2,就说for x in (1,2):。 -
我的那部分代码是针对页数的。如果您查看它下面的行,它只适用于 URL。它不会影响我所指的问题。
-
你能显示一个示例输出吗? “仅第一条记录”可以表示多种含义,例如只有一个输出作为一个记录,或者有多个输出重复同一记录。
-
@burningalc 示例现已附加。
-
productlist_length的值是否正确?
标签: python selenium-webdriver web-scraping xpath css-selectors