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

我在尝试使用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
[email protected]/components/command-processor.js:12517)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/ozmatt/appdata/local/temp/tmpupncqr/extensions/fxdr
[email protected]/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/ozmatt/appdata/local/temp/tmpupncqr/extensions/fxdriv
[email protected]/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")))

Refer to this用于显式等待的用法。

Javascript-从当前网址中删除查询字符串 - javascript

单击提交按钮后,我需要从网址中删除查询字符串值。我可以用jQuery做到这一点吗?当前网址:siteUrl/page.php?key=value 页面提交后:siteUrl/page.php 实际上,我已经从另一个带有查询字符串的页面着陆到当前页面。我需要在页面首次加载时查询字符串值以预填充一些详细信息。但是,一旦我提交了表格,我就需要删除查询字符串值。我已…

Mongo汇总 - javascript

我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …

JavaScript中的字符串评估函数 - javascript

            JavaScript中是否有任何内置函数,例如Python中的eval内置函数?注意:eval函数将方程式作为字符串并返回结果。例如,假设变量x为2,则eval("2x+5")返回9。 参考方案 是的,JavaScript中也有eval函数。此外,该声明应有效用于评估,即eval("2*x+5"…

如何在JQuery中操作JSONArray - javascript

我有一个php函数,它以JSON返回此代码{"0":{"title":"Dans l\u2019appartement"},"1":{"title":"A l\u2019a\u00e9roport - D\u00e9part de B\u00e9at…

在两个值之间匹配并返回正则表达式 - javascript

我正在尝试使用正则表达式从字符串中获取值,该值是tt="和"&之间的文本的值因此,例如,"tt="Value"&"我只想从中得到单词"Value"。到目前为止,我已经有了:/tt=.*&/这给了我"tt=Value"&,然后,要…