Expected_Conditions返回TimeoutException,同时使用element_to_be_clickable方法通过selenium& python 3

时间:2018-05-09 15:54:14

标签: python selenium-webdriver

我正在尝试使用selenium和python 3进行自动化,下面是我尝试过的代码:

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="download"]/span'))
)
element.click()

HTML:

<a href="javascript:void(0);" 
onclick="if(!alen_Button_checkClick('download',event)){return true;};return 
htmlbSL(this,2,'download:downloadBtn')" 
onkeypress="if(!alen_Button_checkClick('download',event)){return 
true;};return htmlbSL(this,2,'download:downloadBtn')" class="urBtnStd1 urV" 
id="download" ct="Button" title="Download To Excel" style="white- 
space:nowrap;">
    <span class="urBtnPadding">
        Download To Excel
    </span>
</a>

我想点击“下载到Excel按钮”,但每次我执行代码时,无论是通过xpath还是类搜索,它都不会执行,并且当我增加超时时会发出超时错误,这对我没有帮助或者我使用{ {1}}这也没有帮助。

stacktrace:

Driver.wait

我在这里缺少什么?

编辑2:

EC.element_to_be_clickable((By.XPATH, '//*[@id="download"]/span'))
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
  selenium.common.exceptions.TimeoutException: Message

编辑2的Stacktrace:

time.sleep(5)
if driver.find_element_by_id('download'):
    print ("Element exists")
time.sleep(5)
if driver.find_element_by_xpath((("//a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']"))):
    print ("Element exists1")

1 个答案:

答案 0 :(得分:0)

要点击带有文字的元素下载到Excel ,您必须引导 WebDriverWait ,并且可以使用以下任一选项:

  • CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.urBtnStd1.urV#download[title='Download To Excel']>span.urBtnPadding"))).click()
    
  • XPATH

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']"))).click()