Python和Selenium以动态形式填充隐藏字段

时间:2017-03-03 11:22:04

标签: python selenium ui-automation

我正在尝试通过航空公司网站自动运行。它需要登录,我使用Selenium在Python中设法做到了。

但填写的表格有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引起了我的问题,但我不确定。

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

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

该部分的代码在这里:

    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)