在用Python Selenium单击另一个按钮后出现单击子元素按钮?

问题描述 投票:0回答:1

当我尝试单击仅在单击父按钮后才出现的按钮时,Selenium webdriver的click()功能在Python中遇到了麻烦。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

url = "https://law.lexmachina.com/cases/?pending-from=2000-01-01&pending-to=2000-02-01&filters=true&tab=summary&view=analytics&cols=475"
driver.get(url)
driver = webdriver.Chrome()
elem0 = driver.find_element_by_id('export-icon-container') # this works

all_children_by_css = elem0.find_elements_by_css_selector("*") # this works, but doesn't click on the sub-button (XLS) one successfully when I run below... 
all_children_by_css[0].click() # this just makes the parent button's little window appear and disappear, the same as elem0.click() does.

>>> all_children_by_css[0] # this is the webElement that I thought was for the XLS button
<selenium.webdriver.remote.webelement.WebElement (session="6b4a559408fa4d512f8596759d81eaf7", 
element="d83be2ca-c879-4706-85ef-db7120d345a3")>

[基本上,我想通过Webdriver导出XLS文件,以便以后可以在带有数据过滤器URL的循环中执行此操作。

[我在下面提供了带注释的屏幕截图,其中详细说明了我要单击的按钮以及与之关联的检查代码。

Inspected code for the parent button of interest

Inspected code for the parent button of interest when hovering over it

Inspected code for the parent button of interest when clicking on it

Inspected code for the newly-appeared button of interest (the XLS one). I can't get the Webdriver to click it.

python selenium-webdriver xpath click inspect
1个回答
0
投票

我建议您查看页面上是否有API可直接导出。如果没有,您可能需要动态等待。您可以尝试以下类似方法。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
url = "https://law.lexmachina.com/cases/?pending-from=2000-01-01&pending-to=2000-02-01&filters=true&tab=summary&view=analytics&cols=475"
driver.get(url)

#Click on Export Icon
elem0 = driver.find_element_by_id('export-icon-container').click() 

#Wait for XLS option to show up
wait = WebDriverWait(driver, 10)
ExportOption= wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@data-action='export-item'][contains(text(),'XLS')]")))
ExportOption.click()
© www.soinside.com 2019 - 2024. All rights reserved.