如何在Selenium Python中等待非空输入字段

时间:2017-10-17 11:26:08

标签: python-3.x selenium phantomjs

我正在尝试使用Python中的Selenium在https://www.mastercard.us/en-us/consumers/get-support/convert-currency.html中自动运行货币转换器。这是我到目前为止所得到的:

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

link1 = 'https://www.mastercard.us/en-us/consumers/get-support/convert-currency.html'

driver1 = webdriver.PhantomJS()
driver1.get(link1)

script = """ var select = arguments[0];
             for(var i = 0; i < select.options.length; i++) {
               if(select.options[i].value == arguments[1]) {
                 select.options[i].selected = true;
               }
             }
         """

driver1.find_element_by_id('getDate').send_keys('05-Sep-2017')                                                                                                        

select = driver1.find_element_by_id('firstID')                                                                                                                        
driver1.execute_script(script, select, 'USD');                                                                                                                                                                                                                                                                                              

driver1.find_element_by_name('txtTAmt').send_keys('1.00')                                                                                                             
driver1.find_element_by_name('txtBankFee').send_keys('0.00')                                                                                                                                                                                                                                                                                

select = driver1.find_element_by_id('newID')                                                                                                                          
driver1.execute_script(script, select, 'EUR');                                                                                                                                                                                                                                                                                              

driver1.find_element_by_id('btnSubmit').click()                                                                                                                                                                                                                                                                                             

wait = WebDriverWait(driver1, 100)                                                                                                                                    
element = wait.until(EC.presence_of_element_located((By.XPATH,                                                                                                                                  
                     '//*[@name="txtCardAmt" and text() != ""]')))                                                                                          
print(element.text)  

问题是字段“txtCardAmt”永远不会被填充,我收到超时异常。我的问题是,我怎么能等待服务器完成计算?

PS:我知道有更简单的方法可以使用Select类选择选项,但是在这个网站中它们由于某种原因不起作用。

1 个答案:

答案 0 :(得分:0)

您的问题是,您要等到名称为 txtCardAmt 的元素的文字不为空。问题是这始终是真的。

如果您看一下感兴趣的html:

<input type="text" name="txtCardAmt" ng-model="mcz.txtCardAmt" 
class="mczreadonly ng-pristine ng-valid mczblue" placeholder="0"
readonly="readonly" disabled="">

你可以看到没有文字。

您要查找的信息(在html中不可见)位于属性中:

enter image description here

在我的例子中, 7.38

所以:

elem = driver1.find_element_by_name('txtCardAmt')
value = elem.get_attribute("value")
print(value)

您选择日期和货币的代码不起作用。在我的例子中,我使用xpath来做到这一点。我确信有更好的方法来完成这项任务。我使用了浏览器检查器工具返回的xpath。

整个例子:

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

link1 = 'https://www.mastercard.us/en-us/consumers/get-support/convert-currency.html'
driver1 = webdriver.PhantomJS(executable_path=r'/pathTo/phantomjs')
driver1.get(link1)

driver1.find_element_by_id('getDate').click()
wait = WebDriverWait(driver1, 20)
wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div[1]/div/div/div/div[2]/div[3]/div/div/div[2]/div/div/div/a[1]/span")))
driver1.find_element_by_xpath("/html/body/div[1]/div/div/div/div[2]/div[3]/div/div/div[2]/div/div/div/a[1]/span").click()
driver1.find_element_by_xpath("//*[@id='transactiondatepicker']/div/table/tbody/tr[2]/td[3]/a").click()


#select = driver1.find_element_by_id('firstID')
#driver1.execute_script(script, select, 'USD');
driver1.find_element_by_xpath("//*[@id='mczRowC']/div[2]/button").click()

wait.until(EC.presence_of_element_located((By.XPATH,"//*[@id='mczRowC']/div[2]/div/ul/li[146]/a")))
driver1.find_element_by_xpath("//*[@id='mczRowC']/div[2]/div/ul/li[146]/a").click()


driver1.find_element_by_name('txtTAmt').send_keys('1.00')
driver1.find_element_by_name('txtBankFee').send_keys('2.00')

#select = driver1.find_element_by_id('newID')
#driver1.execute_script(script, select, 'EUR');
driver1.find_element_by_xpath("//*[@id='mczRowD']/div[2]/button").click()
wait.until(EC.presence_of_element_located((By.XPATH,"//*[@id='mczRowD']/div[2]/div/ul/li[49]/a")))
driver1.find_element_by_xpath("//*[@id='mczRowD']/div[2]/div/ul/li[49]/a").click()


driver1.find_element_by_id('btnSubmit').click()

time.sleep(3)
elem = driver1.find_element_by_name('txtCardAmt')
value = elem.get_attribute("value")
print(value)