繁体   English   中英

使用Python和Selenium WebDriver等待并单击可见按钮

[英]Using Python and Selenium WebDriver to wait and click on a visible button

我在尝试使用Webdriver单击按钮时遇到了麻烦。 在先前字段中输入值之前,该按钮不可见。 我尝试添加睡眠和明确的等待,但仍然没有运气。

我认为这可能与页面javascript有关,但是我的技能还没有扩展到那么远。 我仍然在为我的丑陋代码学习策略。

count=1
while count < 3:
time.sleep(2)
# Not the best way to select the button - but it works for now!
elem = driver.find_element_by_tag_name("button").click()
#Clear default amount
elem = driver.find_element_by_name("amount")
elem.send_keys(Keys.BACKSPACE)
elem.send_keys(Keys.BACKSPACE)
elem.send_keys(Keys.BACKSPACE)
elem.send_keys(Keys.BACKSPACE)
elem.send_keys(Keys.BACKSPACE)
elem.send_keys(Keys.BACKSPACE)
elem.send_keys("0.04")
print 'Entered Amount'
time.sleep(1)
elem.send_keys(Keys.TAB)

time.sleep(3)
elem.send_keys(Keys.TAB)

time.sleep(3)
elem.send_keys("\n")

# This finds the button - but it isn't visible
# elem = driver.find_element_by_tag_name("button").click()
time.sleep(6)
print 'Number of Payments = ', count
count = count + 1
print 'Finished!'

网站代码如下所示:

<button type="button" class="btn alpha centred-form-button ng-binding" ng-click="accountsPayCtrl.submit()" ng-disabled="!accountsPayCtrl.paymentSubmitted &amp;&amp;
          (!paymentForm.$valid || !accountsPayCtrl.inAmount || !accountsPayCtrl.payToken)" tabindex="0" aria-disabled="false">
        Pay $0.04 now
      </button>

毫无疑问,也有更优雅的方法可以达到最终结果!

我得到的错误是:

Traceback (most recent call last):
File "C:\MW_Test\energyaust_Explicit_Wait.py", line 43, in <module>
elem = driver.find_element_by_tag_name("button").click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 75, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 454, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be inter
acted with
Stacktrace:
at fxdriver.preconditions.visible (file:///c:/users/ozmatt/appdata/local/temp/tmpupncqr/extensions/fxdriver@googleco
de.com/components/command-processor.js:9981)
at DelayedCommand.prototype.checkPreconditions_ (file:///c:/users/ozmatt/appdata/local/temp/tmpupncqr/extensions/fxd
river@googlecode.com/components/command-processor.js:12517)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/ozmatt/appdata/local/temp/tmpupncqr/extensions/fxdr
iver@googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/ozmatt/appdata/local/temp/tmpupncqr/extensions/fxdriv
er@googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///c:/users/ozmatt/appdata/local/temp/tmpupncqr/extensions/fxdriver@goog
lecode.com/components/command-processor.js:12481)

您(几乎)在任何情况下都不应使用sleep 相反,Selenium API为您提供了隐式和显式的等待。 从硒文档中:

隐式等待隐式等待是告诉WebDriver在尝试查找一个或多个元素(如果不是立即可用)时轮询DOM一定时间。 默认设置为0。设置后,将在WebDriver对象实例的生存期内设置隐式等待。

对于显式等待:

显式等待显式等待是您定义的代码,用于在继续执行代码之前等待特定条件发生。 最糟糕的情况是Thread.sleep(),它将条件设置为要等待的确切时间段。 提供了一些方便的方法,可以帮助您编写仅等待所需时间的代码。 WebDriverWait与ExpectedCondition结合是实现此目的的一种方法。

现在,就您而言,您需要的是使元素可见,或者由于您需要单击它,因此使其可单击:

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "myDynamicElement")))

请参阅此以了解显式等待的用法。

谢谢你们的建议。 我有一位同事帮助我解决了我的问题,并认为我可以在这里添加它,以帮助像我这样的下一个新手。

事实证明,由于没有找到按钮的细节,我实际上是在寻找另一个隐藏的按钮。 我觉得很傻,但这是一个很好的教训!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM