[英]xpath - clicking all buttons
我的代码进入一个网页,这个页面有几行记录。 单击时,每一行都会显示一个下拉列表。
我的代码启动了一次点击,但出于某种原因只针对网页的第一行?
我怎样才能让我的代码在遇到每条记录时执行一次点击。 不仅仅是第一次进入?
from selenium import webdriver
from bs4 import BeautifulSoup
import time
driver = webdriver.Chrome()
driver.get(f'https://library.iaslc.org/conference-program?product_id=24&author=&category=&date=&session_type=&session=&presentation=&keyword=&available=&cme=&page=1')
time.sleep(4)
page_source = driver.page_source
soup = BeautifulSoup(page_source,'html.parser')
productlist=soup.find_all('div',class_='accordin_title')
for j in productlist:
title=j.find('h4').text.strip()
buttonToClick=driver.find_element_by_class_name('sign')
buttonToClick.click()
time.sleep(5)
driver.find_element_by_class_name('sign')
命令在整个页面上搜索第一个具有sign
类名称的元素。 这就是为什么这总是会给你相同的,第一个元素。 而您应该在j
元素内搜索。
h4
标题也是如此。
为了获得标题并点击手风琴内的按钮,试试这个:
productlist=soup.find_all('div',class_='accordin_title')
for j in productlist:
title=j.find_element_by_xpath('.//h4').text.strip()
buttonToClick=j.find_element_by_xpath('.//div[@class="sign"]')
buttonToClick.click()
time.sleep(5)
UPD
让我们尝试纯硒解决方案
driver.get(f'https://library.iaslc.org/conference-program?product_id=24&author=&category=&date=&session_type=&session=&presentation=&keyword=&available=&cme=&page=1')
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.