2017-03-03 92 views
1

我想通过航空公司网站自动运行。它需要登录,我已经使用Selenium在Python中进行了设置。Python和硒填充动态形式与隐藏字段

但填写表格有2个问题。 1.选择与Selenium出发城市失败,因为该字段是隐藏的。我已经通过使用javascript将其设置为可见。 2.一旦选择出发城市,目的城市字段只填充选项。使用硒,该网站似乎并不接受离开城市已被选中,所以名单永远不会填充。

这是从网站的代码:

   <div class="input depart-city select combobox no-regions"> 
       <label for="departCity" class="visuallyhidden">Departure City </label> 
       <select name="departCity" id="departCity" title="Departure City" style="display: none;"> 
    <option value="">Departure City</option> 
    <option value="FRA">Frankfurt</option> 
    <option value="MUC">Munich</option> 
</select> 
<input placeholder="Departure City" title="Departure City" autocomplete="off" class="ui-autocomplete-input ui-widget ui-widget-content" role="textbox" aria-autocomplete="list" aria-haspopup="true"><button type="button" class="ui-button icon-arrow-down ui-button-icon" tabindex="-1" title="Show All Items"> </button> 

      <ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: 0px; left: 0px; display: none;"></ul></div> 

      <div class="input destination-city select combobox" id="destinationLinkSelect"> 
       <label for="destCity" class="visuallyhidden">Destination City </label> 


        <select name="destCity" id="destCity" title="Destination City " style="display: none;"> 
         <option value="">Destination City </option> 
        </select><input placeholder="Destination City " title="Destination City " autocomplete="off" class="ui-autocomplete-input ui-widget ui-widget-content" role="textbox" aria-autocomplete="list" aria-haspopup="true"><button type="button" class="ui-button icon-arrow-down ui-button-icon" tabindex="-1" title="Show All Items"> </button> 

      <ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: 0px; left: 0px; display: none;"></ul></div> 

我的代码如下所示:

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

driver = webdriver.Firefox() 
# browser.set_window_size(1124, 850) 
driver.get("https://www.flysaa.com/de/en/voyagerLogin.secured") 
assert "Voyager" in driver.title 
driver.find_element_by_name("voyagerId").send_keys("######") 
driver.find_element_by_name("pin").send_keys("####") 
driver.find_element_by_name("loginButton").click() 
time.sleep(5) 
try: 
    element = WebDriverWait(driver, 10).until(
     EC.presence_of_element_located((By.ID, "ad1")) 
    ) 
except: 
    print "Page didn't load!" 
driver.find_element_by_partial_link_text("South Africa(English)").click() 
Select(driver.find_element_by_id("country")).select_by_value("DE") 
driver.find_element_by_id("changeRegionAndLanguages").click() 
time.sleep(3) 

driver.find_element_by_id("voyagerFlightSearch").send_keys(Keys.TAB) 
driver.execute_script("document.getElementById('departCity').style.display='inline-block';") 
driver.execute_script("document.getElementById('destCity').style.display='inline-block';") 
driver.execute_script("document.getElementById('departDay').style.display='inline-block';") 
driver.execute_script("document.getElementById('departMonthYear').style.display='inline-block';") 
driver.execute_script("document.getElementById('chkReturn').style.display='inline-block';") 
driver.execute_script("document.getElementById('preferredClass').style.display='inline-block';") 
driver.find_element_by_id("departCity").send_keys("Frankfurt" + Keys.TAB) 
time.sleep(3) 
Select(driver.find_element_by_name("destCity")).select_by_value("JNB") 
driver.find_element_by_id("destCity").send_keys(Keys.TAB) 
Select(driver.find_element_by_name("departDay")).select_by_value("01") 
#driver.find_element_by_name("departMonthYear").select_by_value("Oct-2017") 
#driver.find_element_by_name("chkReturn").click() 
#driver.find_element_by_name("preferredClass").select_by_value("1") 
time.sleep(10) 
#assert "No results found." not in driver.page_source 
driver.quit() 

任何想法?我认为唱段已经导致我的问题,但我不确定。

+0

如果GUI测试是基于与隐藏要素直接相互作用,你是不是做什么是真正的用户会做网页上。这个事实使得测试几乎没有意义。尝试通过做一些真正的用户也可以做到的事情来实现你想要的。 – kriegaex

+0

我同意,这似乎是本网站设计的方式。它是隐藏的,除非你点击它。 –

+0

然后,点击它。 ;-) 祝你好运! – kriegaex

回答

0

您可以使用下面的代码,以避免应用JavaScript以及模拟真实用户行为:

# Click arrow-button to open drop-down 
driver.find_element_by_xpath('//button[@title="Show All Items"]').click() 
# Select required option 
driver.find_element_by_link_text('Frankfurt').click() 

以同样的方式,您可以处理“目标”字段:

# Click arrow-button to open drop-down 
driver.find_element_by_xpath('(//button[@title="Show All Items"])[2]').click() 
# Select required option 
driver.find_element_by_link_text('Calgary').click() 
+0

谢谢,它正确地选择了列表,但似乎没有点击法兰克福选项。它会突出显示它,但不会点击它。那么第二个列表不会被填充。 –

0

我设法使它使用@安德森的答案工作。但是,该网站无法从列表中选择,或者在组合框中输入。它需要箭头来选择选项,就像鼠标点击它一样。

我对那部分代码是在这里:

driver.find_element_by_id("voyagerFlightSearch").send_keys(Keys.TAB) 
    driver.find_element_by_xpath('//button[@title="Show All Items"]').click() 
    driver.find_element_by_class_name('ui-widget').send_keys(Keys.ARROW_DOWN + Keys.ARROW_DOWN + Keys.ARROW_DOWN + Keys.TAB) 
    time.sleep(1) 
    driver.find_element_by_xpath('(//button[@title="Show All Items"])[2]').click() 
    driver.find_elements_by_class_name('ui-widget')[2].send_keys("Johannesburg (OR Tambo)" + Keys.TAB)