无法使用selen

时间:2018-03-30 20:33:43

标签: python python-3.x selenium selenium-webdriver web-scraping

我在python中用selenium编写了一个脚本,点击网页中的某个链接下载excel文件。但是,当我执行我的脚本时,它会抛出timeout异常。我怎样才能使它工作?任何帮助将不胜感激。

链接到网站:webpage

脚本我尝试过:

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get('replace_with_above_link')

item = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".hasmore #dlink")))
item.click()
driver.quit()

包含下拉选项的Html元素:

<li class="hasmore drophover"><span>Share &amp; more</span><div><ul><li><button class="tooltip" tip="Use a customizable report creator that can<br>output HTML, CSV, or a shareable link." id="share_on_ajax_result_table">Modify &amp; Share Table</button></li><li><button class="tooltip" tip="Get a bit of widget code to emed this table on your site">Embed this Table</button></li><li><button class="tooltip" tip="Convert the table below to comma-separated values<br>suitable for use with excel">Get as Excel Workbook (experimental)</button><a id="dlink" style="display: none;"></a></li><li><button class="tooltip" tip="Export table as <br>suitable for use with excel">Get table as CSV (for Excel)</button></li><li><button class="tooltip" tip="">Strip Mobile Formatting</button></li><li><a id="a_ajax_result_table" name="ajax_result_table" href="#ajax_result_table::none">Copy Link to Table to Clipboard</a></li><li><button class="tooltip" tip="">About Sharing Tools</button></li><li><button class="tooltip" tip=""><a href="https://www.youtube.com/watch?v=MWapXbaWs_U&amp;feature=youtu.be" target="_blank">Video: SR Sharing Tools &amp; How-to</a></button></li><li><button class="tooltip" tip=""><a href="https://www.youtube.com/watch?v=JkDLV0roT14&amp;feature=youtu.be" target="_blank">Video: Stats Table Tips &amp; Tricks</a></button></li></ul></div></li>

该网页中该文件的位置(所需链接标有铅笔): enter image description here

2 个答案:

答案 0 :(得分:1)

目标链接已隐藏,因此等待其可见性始终会失败。您应该尝试处理button节点:

item = wait.until(EC.visibility_of_element_located((By.XPATH, "//li[span='Share & more']")))
item.click()
wait.until(lambda driver: "drophover" in item.get_attribute("class"))
item.find_element_by_xpath("//button[.='Get as Excel Workbook (experimental)']").click()

答案 1 :(得分:0)

当您尝试点击带有文本的链接获取为Excel工作簿(实验性)时,根据您的评论,您已经能够点击“共享”和“更多”链接第一个位置,发现它正常工作接下来您想要的<a>标记元素包含属性样式设置为 display:none; 。因此,要调用click()进行下载,您可以使用以下代码块:

Get_as_Excel_Workbook_link = driver.find_element_by_xpath("//li[@class='hasmore drophover']//ul//li//a[@id='dlink']")
driver.execute_script("arguments[0].removeAttribute('style')", Get_as_Excel_Workbook_link)
Get_as_Excel_Workbook_link.click()

更新A

根据你的评论:

comment

  • 我不确定您使用的xpath是否为有效

    "//li[a[@id='dlink']]/a"
    
  • 您尝试使用:

    Get_link = driver.find_element_by_xpath("//li[a[@id='dlink']]/a") 
    print(Get_link.get_attribute("outerHTML"))
    

但为什么呢?有必要吗?

  • 根据我的研究和分析,您可以放心,您在正确的地方。请参阅您共享的 HTML 格式化版本以及我提供的 xpath 的解析。

&#13;
&#13;
<li class="hasmore drophover"><span>Share &amp; more</span>
  <div>
    <ul>
      <li><button class="tooltip" tip="Use a customizable report creator that can<br>output HTML, CSV, or a shareable link." id="share_on_ajax_result_table">Modify &amp; Share Table</button></li>
      <li><button class="tooltip" tip="Get a bit of widget code to emed this table on your site">Embed this Table</button></li>
      <li><button class="tooltip" tip="Convert the table below to comma-separated values<br>suitable for use with excel">Get as Excel Workbook (experimental)</button>
        <a id="dlink" style="display: none;"></a>
      </li>
      <li><button class="tooltip" tip="Export table as <br>suitable for use with excel">Get table as CSV (for Excel)</button></li>
      <li><button class="tooltip" tip="">Strip Mobile Formatting</button></li>
      <li><a id="a_ajax_result_table" name="ajax_result_table" href="#ajax_result_table::none">Copy Link to Table to Clipboard</a></li>
      <li><button class="tooltip" tip="">About Sharing Tools</button></li>
      <li><button class="tooltip" tip=""><a href="https://www.youtube.com/watch?v=MWapXbaWs_U&amp;feature=youtu.be" target="_blank">Video: SR Sharing Tools &amp; How-to</a></button></li>
      <li><button class="tooltip" tip=""><a href="https://www.youtube.com/watch?v=JkDLV0roT14&amp;feature=youtu.be" target="_blank">Video: Stats Table Tips &amp; Tricks</a></button></li>
    </ul>
  </div>
</li>
&#13;
&#13;
&#13;

  • 所以你看到的结果非常正确。现在,为了您的理解,我已在预期标签中插入了一些 MyLink 文本:

    <a id="dlink" style="display: none;"></a>
    
  • 转换为:

    <a id="dlink" style="display: none;">MyLink</a>
    
  • 查看结果:

result

  • 再次检查我的解决方案我可以确保有效。

更新B

无法找到元素是一个很好的调试消息,除了&#34; display:none;&#34; 你已经在实际问题上拉了一块地毯提到首先点击了分享和更多链接,发现它正常运行。当我尝试点击链接时出现麻烦。

如果您观察 HTML ,则该元素位于 class =&#34;工具提示&#34; 内,因此您需要按如下方式诱导服务员:

//perform click on the link Share&more
Get_as_Excel_Workbook_link = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//li[@class='hasmore drophover']//ul//li//a[@id='dlink']")))
driver.execute_script("arguments[0].removeAttribute('style')", Get_as_Excel_Workbook_link)
Get_as_Excel_Workbook_link.click()