Python selenium webdriver扩展accordioncontainer(取两个)

2024-04-26 02:28:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在构建一个scraper,它工作得很好,但需要我手动扩展隐藏数据,然后才能成功地将其刮取。我已经检查了网页的源代码和数据是在3个不同的手风琴容器

有一个可单击的表格标题横幅,其中包含大量的标题元素,这些标题元素也被刮取。我已经尝试了标题中可能包含clickable元素的所有xpath,以及如下代码:

driver.find_element_by_xpath('//*[@id="income-statement-accordion"]/thead/tr/th[1]').click

但没有可能的xpath组合扩展表。 当我查看源代码时,当我单击标题时,源代码中唯一出现变化的部分如下

<table _ngcontent-ng-lseg-c34="" class="full-width income-statement swipable-table accordion-container" id="income-statement-accordion"> == $0

对此的更改(我不确定== $0的重要性):

<table _ngcontent-ng-lseg-c34="" class="full-width income-statement swipable-table accordion-container active-accordion" id="income-statement-accordion">

我在这里看到了一个类似的问题:Open an accordion with selenium in python。然而,尝试类似的方法并没有任何作用,尽管无可否认,提交时没有错误并返回none

driver.execute_script("document.getElementById('income-statement-accordion').class='full-width income-statement swipable-table accordion-container active-accordion';")

我错过了什么


Tags: 数据id元素标题源代码containertablewidth
1条回答
网友
1楼 · 发布于 2024-04-26 02:28:57

这似乎有效

elements = driver.find_elements_by_xpath("//span[contains(@class, 'accordion-toggler')]")
for element in elements:
    driver.execute_script("arguments[0].click();", element)

这将扩展所有手风琴部分

如果您只想针对各个部分

element = driver.find_element_by_xpath("//table[@id='income-statement-accordion']//span[contains(@class, 'accordion-toggler')]")
driver.execute_script("arguments[0].click();", element)

我们需要使用javascript来单击元素,因为selenium没有将它们归类为“可交互的”

相关问题 更多 >