Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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 等待站点加载Selenium后如何从a href下载_Python_Html_Selenium - Fatal编程技术网

Python 等待站点加载Selenium后如何从a href下载

Python 等待站点加载Selenium后如何从a href下载,python,html,selenium,Python,Html,Selenium,背景: 我有一些Python Selenium代码,等待网站加载(有延迟)。加载网站后,我想单击href/下载链接 Python Selenium: browser = webdriver.Chrome('C:/full/path/to/chromedriver.exe') browser.get(MAIN_WEBSITE + pdf) delay = 20 # seconds try: myElem = WebDriverWait(browser, de

背景:

我有一些Python Selenium代码,等待网站加载(有延迟)。加载网站后,我想单击href/下载链接

Python Selenium:

   browser = webdriver.Chrome('C:/full/path/to/chromedriver.exe')

   browser.get(MAIN_WEBSITE + pdf)
   delay = 20 # seconds
   try:
       myElem = WebDriverWait(browser, delay).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='btn-group']")))
       print("Page is ready!")
       # Now the site is loaded - I can download the pdf - but I am stuck!

   except TimeoutException:
       print("Loading took too much time!")
加载后的HTML代码

<div class="btn-group">
<a type="button" class="btn btn-primary btn-user" onclick="AiD('182030801')" href="/download.pdf?id=182030801&amp;h=917901e6659ad5eb53970aecf687b53e&amp;u=cache&amp;ext=pdf" target="_blank" style="border-top-left-radius: 3px;border-bottom-left-radius: 3px;">
[...]
</div>

使用类似的方法获取pdf页面链接

import urllib

try:
     myElem = WebDriverWait(browser, delay).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='btn-group']/a")))
     urllib.retrieve(driver.current_url+myElem.get_attribute('href'))     
except TimeoutException:
   print("Loading took too much time!")

Xpath最后需要一个/a和一个.click()。@arundeepchohan我意识到-更改/更新了代码-但仍然不起作用:(可能需要使用myElem.getAttribute('href'),而不是与driver.current\u url.just driver.get(driver.current\u url+myElem.getAttribute('href')结合使用)然后单击后面的下载链接。@arundeepchohan
print(myElem.getAttribute(href'))AttributeError:“WebElement”对象没有属性“getAttribute”
::/我不认为urllib已经有了urlretrieve…已经有了,而且我认为如果我
,效果会更好。单击()
按钮…..click()但是有一个单击操作。onclick=“AiD('182030801')”我不能真正测试,所以请尝试一下。
import urllib

try:
     myElem = WebDriverWait(browser, delay).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='btn-group']/a")))
     urllib.retrieve(driver.current_url+myElem.get_attribute('href'))     
except TimeoutException:
   print("Loading took too much time!")