Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法使用js后端单击下载链接_Python_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 无法使用js后端单击下载链接

Python 无法使用js后端单击下载链接,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,网站上有一个下载csv文件的链接。链接在表中,但实际下载链接隐藏 <div id="testjs"> <div test-example=""> <table test-example=""> <tr test-example=""> <th test-example="">Car</th> <th test

网站上有一个下载csv文件的链接。链接在表中,但实际下载链接隐藏

<div id="testjs">
    <div test-example="">
        <table test-example="">
            <tr test-example="">
                <th test-example="">Car</th>
                <th test-example="">File</th>
            </tr>
            <tr test-example="">
                <td test-example="">Ford</td>
                <td test-example="">
                    <a test-example="" href="#">ford.csv</a>
                </td>
            </tr>
        </table>
    </div>
</div>
当上面的最后一行运行时,脚本返回:

<selenium.webdriver.remote.webelement.WebElement (session="<example session string>", element="<example element string>")>

如何下载文件?

显然,以下代码行没有任何问题:

driver.find_element_by_link_text('ford.csv')
然而,在这一点上值得一提的是,点字符
总是具有特殊的效果/意义


假设您打算
在文本为ford.csv的元素上单击()
,该元素与文本为ford的元素相邻,作为解决方案,您可以:

  • 将文本
    ford.csv
    分为两部分
    ford
    csv
    并在
  • 当您打算调用
    click()
    时,必须使
    元素的WebDriverWait成为可点击的()
  • 您可以使用以下任一选项:

    • 使用
      CSS\u选择器

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "tr[test-example] td:nth-child(2)>a[test-example]"))).click()
      
    • 使用
      XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'ford') and contains(.,'csv')]"))).click()
      
    • 注意:您必须添加以下导入:

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

  • 在新的硒版本中不支持幻灯片,请考虑用户的其他驱动程序。对于你的问题,JS中有一个点击按钮的错误

    试试这个,我在selenium库中找到了单击按钮的方法

    item = driver.find_element_by_link_text('ford.csv')
    ActionChains(driver).move_to_element(item).click().perform()
    

    使用Chrome无头浏览器而不是PhantomJS您使用Chrome有什么解决方案吗?我使用Chrome驱动程序部分解决了我的问题,但没有使用Headless。德班詹布的解决方案确实奏效了。然而,有一个“允许cookies”的弹出窗口阻碍了我必须首先点击的下载链接。无头chrome的问题在于没有下载任何东西。当chrome不是headless时,文件被下载了。检查headless chrome的这个答案,看看这是否有帮助好的,我尝试过用这种方法来处理headless chrome,但没有任何效果。有什么想法吗?通过这些方法,我得到了相同的错误:selenium.common.exceptions.ElementClickInterceptedException:Message:element单击intercepted:element在点(X,Y)处不可单击。其他元素将收到click:To address element ClickInterceptedException跟随讨论1)2)谢谢问题是一个“允许cookies”弹出窗口正在阻碍。因此,我使用您的解决方案首先允许cookies,然后下载文件。
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    item = driver.find_element_by_link_text('ford.csv')
    ActionChains(driver).move_to_element(item).click().perform()