如何通过Python用硒单击隐藏的按钮 [英] How to click on a hidden button with selenium through Python

查看:124
本文介绍了如何通过Python用硒单击隐藏的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在具有以下来源的页面上单击Upload from my Computer按钮.

I’m trying to click an Upload from my Computer button on a page that has the source below.

我正在使用硒,并尝试了几种不同的方法.下面将对过去失败的方法以及当前失败的方法进行注释.当前方法返回的错误如下.

I’m using selenium and tried several different approaches. The past failed approaches are commented out below, along with the current failed approach. The error that’s returned with the current approach is below.

任何人都可以看到问题所在并提出解决方案的建议吗?我是硒的新手,所以如果有人可以提供一些有关html的功能以及他们的代码如何解决问题的解释,那对我的理解真的很有帮助.

Can anyone see what the issue might be and suggest how to solve it? I’m new to selenium so if someone can provide some explanation of what the html is doing and how their code solves the issue as well it would be really helpful for my understanding.

按钮的HTML代码:

<div class="hidden-xs">
    <label for="fuUploadFromMyComputer" class="hidden">
        Upload from my Computer
    </label>
    <input id="fuUploadFromMyComputer" type="file" name="upload">
    <button id="btnUploadFromMyComputer" 
            class="center-block btn btn-white-fill btn-block " 
            data-resume-type="COMPUTER" type="submit">
        <i class="zmdi zmdi-desktop-mac"></i>
        Upload from my Computer
    </button>
</div>

尝试:

# clicking upload button

# upload_btn = driver.find_element_by_id("fuUploadFromMyComputer")
# upload_btn = driver.find_element_by_css_selector(
#                 '.center-block.btn.btn-white-fill.btn-block')
# upload_btn = driver.find_element_by_link_text('Upload from my Computer')

# upload_btn.click()



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

WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
     (By.CSS_SELECTOR, "div.center-block btn.btn-white-fill.btn-block"))).click()

错误:

---------------------------------------------------------------------------
TimeoutException                          Traceback (most recent call last)
<ipython-input-43-8fd80ff3c690> in <module>()
     14 from selenium.webdriver.support import expected_conditions as EC
     15 
---> 16 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.center-block btn.btn-white-fill.btn-block"))).click()
     17 
     18 time.sleep(3)

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/support/wait.py in until(self, method, message)
     78             if time.time() > end_time:
     79                 break
---> 80         raise TimeoutException(message, screen, stacktrace)
     81 
     82     def until_not(self, method, message=''):

TimeoutException: Message: 

推荐答案

要单击文本为从我的计算机上载的元素,您需要为 WebDriverwait 可以点击的元素,您可以使用以下任一解决方案:

To click on the element with text as Upload from my Computer you need to induce WebDriverwait for the element to be clickable and you can use either of the following solutions:

  • CSS_SELECTOR:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.center-block.btn.btn-white-fill.btn-block#btnUploadFromMyComputer"))).click()

  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='center-block btn btn-white-fill btn-block ' and @id='btnUploadFromMyComputer']"))).click()
    

  • 注意:您必须添加以下导入:

  • Note: You have to add the following imports :

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

  • 这篇关于如何通过Python用硒单击隐藏的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆