如何单击Selenium WebElement列表?

时间:2019-05-06 23:42:12

标签: python selenium

我正在尝试通过吸收所有需要扩展的元素,然后单击它们来扩展一些可折叠的内容。然后,在打开它们后,刮取显示的数据。到目前为止,我正在使用xpath来获取元素列表,

clicks = driver.find_elements_by_xpath("//div[contains(@class, 'collapsableContent') and contains(@class, 'empty')]")

我尝试用一​​个简单的for循环进行迭代:

for item in clicks:
    item.click()

但这似乎不起作用。有什么建议去找?

我要从中获取此信息的特定页面是:https://sports.betway.com/en/sports/sct/esports/cs-go

1 个答案:

答案 0 :(得分:0)

这是您用于打开所有具有折叠的空类的div的代码。

# click on close button in cookies policy (as this is the element which will overlap the element clicks)    
driver.find_element_by_css_selector(".messageBoxCloseButton.icon-cross").click()
# get all the divs (collapsed divs)
links = driver.find_elements_by_xpath("//div[@class='collapsableContent empty']/preceding-sibling::div[@class='collapsableHeader']")
# click on each of those links
for link in links:
    link.location_once_scrolled_into_view
    link.click()