我无法点击网站上的选择下拉菜单

时间:2015-06-12 04:35:18

标签: selenium selenium-webdriver

我无法点击departarrvie下拉菜单表格

  

https://m.flyscoot.com/search

我尝试使用Xpath点击它们,但我没有运气:

@driver.find_element(:xpath,  "//~~~~~" ).click

有没有办法让我手动填写date value

如果我想点击全年,点击鼠标日期是不切实际的。

更实际的解决方案是生成全年的日期,然后填写,但我无法通过硒firefox driver获得它。

使用包含方法的xpath没有运气:

@driver.find_element(:xpath, "//*[contains(text(),'Departure Airport')]")
  

NoSuchElementError:无法找到元素:       [4]撬(#)>到达= @ driver.find_element(:xpath," // [包含(text(),'离境机场')]")       Selenium :: WebDriver :: Error :: NoSuchElementError:无法找到元素:{"方法":" xpath","选择器":" // [含有(文本(),'离开   机场&#39)]"}       来自[remote server] file:/// var / folders / 6d / th4jpm90n1cx8h2l3kr49fr0000gn/T/webdriver-profile20150612-45453-15z20qu/extensions/fxdriver@googlecode.com/components/driver-component.js:0271:in   `FirefoxDriver.prototype.findElementInternal '

     

的webdriver

➜  workspace  gem list selenium-webdriver

*** LOCAL GEMS ***

selenium-webdriver (2.45.0)

4 个答案:

答案 0 :(得分:0)

要离开你可以使用(其他方式,但我不知道你的源代码在这里抓住字符串):

 arrival = find_element_by_xpath("//*[contains(text(),'Departure Airport')")
 departure find_element_by_xpath("//*[contains(text(),'Arrival Airport')")

至于设置日期,我需要查看您的页面源代码。我想这可以使用直接JS或Jquery与driver.execute_script('script here') command.

来完成

答案 1 :(得分:0)

使用以下代码选择机场:

    driver.get("https://m.flyscoot.com/search");

    // click on departure airport
    driver.findElement(By.xpath("//div[@id='departureAirport-button']"))
            .click();

    // Select any of option from the list of airports,Here i m selecting "sydney"
    driver.findElement(
            By.xpath("html/body/div[6]/div/div[2]/div/div[3]/div/div/div/div/div[2]/div[1]/div/div[1]/div[5]/div"))
            .click();

    //Click on done 
    driver.findElement(By.xpath("//div[text()='Done']")).click();

这是选择日期的代码:

    // Click on Departure Date
    driver.findElement(By.xpath("//input[@id='date-depart-display']"))
            .click();


    // select date by clicking on particular date,Here i m selecting today's
    // date.
    driver.findElement(
            By.xpath(".//*[@id='ui-datepicker-div']/div[1]/table/tbody/tr[2]/td[6]/a"))
            .click();

答案 2 :(得分:0)

首先,在与之交互之前等待出发下拉列表

另外,使用Select abstractionselectByVisibleText()(Java):

wait = WebDriverWait(driver, 10);

WebElement departure = wait.until(ExpectedConditions.visibilityOfElementLocated(By.ID("departureAirport")));
Select departureSelect = new Select(select);
departureSelect.selectByVisibleText("Hong Kong (HKG)");

WebElement arrival = driver.findElement(By.ID("arrivalAirport"));
Select arrivalSelect = Select(arrival);
arrivalSelect.selectByVisibleText("Melbourne (MEL)");

Python中的相同逻辑:

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


driver = webdriver.Firefox()
driver.get("https://m.flyscoot.com/search")

wait = WebDriverWait(driver, 10)
departure = wait.until(EC.visibility_of_element_located((By.ID, "departureAirport")))
departure = Select(departure)

departure.select_by_visible_text("Hong Kong (HKG)")

arrival = Select(driver.find_element_by_id("arrivalAirport"))
arrival.select_by_visible_text("Melbourne (MEL)")

答案 3 :(得分:0)

我试过这个代码,它部分起作用(至少日期被选中),但是离开机场的选择不是,我仍然想知道为什么,请看看这是否给你任何开始。如果得到一些解决方案,我会回来的意思。

  Driver.navigate().to("https://m.flyscoot.com/search");
  Driver.findElement(By.id("departureAirport")).click();
  Select Element = new Select(Driver.findElement(By.id("departureAirport")));
  Element.selectByVisibleText("Singapore (SIN)");

  Driver.findElement(By.id("date-depart-display")).click();
  Thread.sleep(10);
  Driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/table/tbody/tr[3]/td[4]/a")).click();