2017-03-03 3 views
1

私は航空会社のサイトでの自動実行を試みています。ログインする必要があります。これは、Seleniumを使用してPythonで管理しています。PythonとSeleniumは、非表示のフィールドで動的フォームを埋めています

ただし、入力するフォームには2つの問題があります。 1. Seleniumで出発都市を選択すると、フィールドが非表示になっているために失敗します。私はこれを目に見えるように設定するためにjavascriptを使用して周りに持っている。 2.出発都市が選択されると、目的地の都市フィールドにはオプションが設定されます。 Seleniumを使用しているサイトでは出発都市が選択されていると受け入れられないため、リストには決して表示されません。

これは、サイトからのコードです:

   <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() 

任意のアイデア?私はAria-haspopupが私の問題を引き起こしていると思うが、わからない。

あなたが JavaScriptを加えないと、実際のユーザーの行動をシミュレートするために、コードの下に使用することができます
+0

場合、あなたは本物のユーザーがページ上でどうなるのかやっていません。この事実により、テストはかなり無意味になります。実際のユーザーができることをやって、あなたが望むものを達成しようとしてください。 – kriegaex

+0

私は同意する、それはこのサイトが設計された方法であるように思われる。あなたがそれをクリックしない限り隠されます。 –

+0

それからそれをクリックしてください。 ;-) がんばろう! – kriegaex

答えて

0

:あなたは「宛先」フィールドを扱うことができるのと同じ方法で

# 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

ありがとう、それは、リストを正しく選択しますが、フランクフルトオプションをクリックしていないようです。ハイライトしますが、クリックしません。したがって、2番目のリストには値が入りません。 –

0

私はそれを作るために管理@アンダーソンの答えを使用して作業します。しかし、サイトはリストからの選択、またはコンボボックスへの入力には対応していません。マウスがマウスをクリックしているかのように、矢印を使用してオプションを選択する必要があります。

そのセクションの私のコードはここにある:GUIテストは、隠し要素との直接的な相互作用に基づいている

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) 
関連する問題