【问题标题】:Click sub-element button when it appears after clicking another button with Python Selenium?使用Python Selenium单击另一个按钮后出现的单击子元素按钮?
【发布时间】:2019-10-07 19:58:11
【问题描述】:

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

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 的循环中执行此操作。

我在下面添加了带注释的屏幕截图,详细说明了我尝试单击的按钮以及与它们相关联的检查代码。

【问题讨论】:

    标签: python selenium-webdriver xpath click inspect


    【解决方案1】:

    我建议查看页面上是否有 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()
    

    【讨论】:

    • 遗憾的是,该网站故意让您一次只能导出 1000 条记录。目标是在循环中获取记录的批次。我尝试了您的解决方案,但收到此错误:&gt;&gt;&gt; ExportOption= wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@data-action='export-item'][contains(text(),'XLS')]")))... Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File ".../lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
    • 第一次点击成功&XLS选项出现在屏幕上了吗?
    • 是的,我已经做到了。当我执行driver.find_element_by_id('export-icon-container') 和执行elem0.find_elements_by_css_selector("*").click() 时,会出现小XLSPDF 导出窗口(我在原始问题中的all_children_by_css 对象)。当我重复相同的调用时,小窗口会关闭,就好像这些函数正在来回切换窗口一样。但是当我做后者(孩子点击呼叫)时,前者(elem0 点击呼叫)不再打开和关闭那个小窗口了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 2020-08-06
    • 2017-04-22
    相关资源
    最近更新 更多