我一直试图遍历页面上具有定义特征的所有svg元素,以单击它们然后进行解析,但是发现只有如何打开第一个元素 你知道怎么打开所有的吗? 任何反馈意见表示赞赏:)
<tr class="MuiTableRow-root">
<td class="MuiTableCell-root jss346 MuiTableCell-alignLeft MuiTableCell-sizeSmall MuiTableCell-body">АМАРИЛ М ТАБЛ. П/ПЛЕН/ОБ. 2МГ+500МГ №30</td>
<td class="MuiTableCell-root jss346 MuiTableCell-alignRight MuiTableCell-sizeSmall MuiTableCell-body">1</td>
<td class="MuiTableCell-root jss346 MuiTableCell-alignLeft MuiTableCell-sizeSmall MuiTableCell-body"><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss348 MuiTypography-colorPrimary"><svg class="MuiSvgIcon-root jss350" focusable="false" viewBox="0 0 24 24" aria-hidden="true" role="presentation"><path d="M18 17H6v-2h12v2zm0-4H6v-2h12v2zm0-4H6V7h12v2zM3 22l1.5-1.5L6 22l1.5-1.5L9 22l1.5-1.5L12 22l1.5-1.5L15 22l1.5-1.5L18 22l1.5-1.5L21 22V2l-1.5 1.5L18 2l-1.5 1.5L15 2l-1.5 1.5L12 2l-1.5 1.5L9 2 7.5 3.5 6 2 4.5 3.5 3 2v20z"></path></svg></a></td>
</tr>
<tr class="MuiTableRow-root">
<td class="MuiTableCell-root jss346 MuiTableCell-alignLeft MuiTableCell-sizeSmall MuiTableCell-body">АМАРИЛ М ТАБЛ. П/ПЛЕН/ОБ. 2МГ+500МГ №30</td>
<td class="MuiTableCell-root jss346 MuiTableCell-alignRight MuiTableCell-sizeSmall MuiTableCell-body">1</td>
<td class="MuiTableCell-root jss346 MuiTableCell-alignLeft MuiTableCell-sizeSmall MuiTableCell-body"><a class="MuiTypography-root MuiLink-root MuiLink-underlineHover jss348 MuiTypography-colorPrimary"><svg class="MuiSvgIcon-root jss350" focusable="false" viewBox="0 0 24 24" aria-hidden="true" role="presentation"><path d="M18 17H6v-2h12v2zm0-4H6v-2h12v2zm0-4H6V7h12v2zM3 22l1.5-1.5L6 22l1.5-1.5L9 22l1.5-1.5L12 22l1.5-1.5L15 22l1.5-1.5L18 22l1.5-1.5L21 22V2l-1.5 1.5L18 2l-1.5 1.5L15 2l-1.5 1.5L12 2l-1.5 1.5L9 2 7.5 3.5 6 2 4.5 3.5 3 2v20z"></path></svg></a></td>
</tr>
以此类推...
我的代码:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options)
driver.get(url)
XPATH = "//*[name()='svg' and contains(@class, 'jss350')]"
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and contains(@class, 'jss350')]"))).click()
driver.quit()
答案 0 :(得分:0)
似乎您在这里错误地使用了XPath
-在@name='svg'
上进行查询将获得具有name
属性svg
的元素,而不是类型为{{1}的WebElements }。
如果要等待所有svg
元素存在,可以执行以下操作:
svg
但是,您还想遍历列表中的元素。您将需要首先找到所有元素,然后进行迭代。您可以尝试以下代码:
WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//svg[contains(@class, 'jss350')]")))
这将等待所有# Wait for all svg elements to be clickable
WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//svg[contains(@class, 'jss350')]")))
# find list of svg elements
svg_elements = driver.find_elements_by_xpath("//svg[contains(@class, 'jss350')]")
# iterate the list and click each one
for element in svg_elements:
element.click()
元素存在,从页面中获取元素列表,迭代每个元素,然后单击它。