2016-01-13 68 views
0

我正在嘗試使用Webdriver單擊一個按鈕。只有在前一個字段中輸入值後,該按鈕纔可見。我嘗試添加睡眠和明確的等待,但仍然沒有運氣。使用Python和Selenium WebDriver等待並單擊一個可見按鈕

我想這可能與頁面JavaScript有關,但我的技能沒有那麼深。我仍然在學習爲我的醜陋的代碼,以便apoligies ...

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/[email protected] 
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/[email protected] 
lecode.com/components/command-processor.js:12481) 
+0

更優雅然後'睡眠'是明確的等待。你能證明你是如何嘗試使用它的? – Guy

回答

1

你不應該使用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顯式等待的用法。

0

感謝您的建議傢伙。我有一位同事幫助我解決了我的問題,並認爲我會在這裏添加它,或許可以幫助像我這樣的下一個新手。

原來,通過找不到詳細信息來找到按鈕,我實際上是在找另一個隱藏的按鈕。我感覺很蠢,但這是一個很好的教訓!

相關問題