如何使用硒处理Phptravels.net网站上的下拉菜单

时间:2018-07-02 19:33:41

标签: java selenium selenium-webdriver

我在https://www.phptravels.net/网站上使用下拉菜单处理句柄时遇到了一些大问题。

我写了这个测试,但是它不起作用,我也不知道如何正确地进行测试。

public class HomePage {

    private Logger logger = LogManager.getRootLogger();

    @FindBy(xpath = "//*[@id=\"li_myaccount\"]/ul")
    private WebElement dropDownMenu;

    @FindBy(xpath = "//*[@id=\"li_myaccount\"]/ul/li")
    private WebElement dropDownMenuOptions;

    public HomePage() {
        PageFactory.initElements(DriverManager.getWebDriver(), this);
    }

    public void clickOnMyAccountDropMenuAndSelectOption(String option) {
        WaitForElement.waitUntilElementIsVisible(dropDownMenu);
        dropDownMenu.click();

        List<WebElement> options = DriverManager.getWebDriver().findElements(By.xpath("//*[@id=\"li_myaccount\"]/ul/li"));

        for(WebElement o: options) {
            if(o.getText().equals(option)) {
                o.click();
                return;
            }
        }   
    }

    public void clickOnLoginLink() {
        WaitForElement.waitUntilElementIsClickable(loginLink);
        loginLink.click();
        logger.info("Clicked on Login link");
    }
}

我的问题是我应该如何更改clickOnMyAccountDropMenuAndSelectOption方法以使测试正确?感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

尝试一下:

public class HomePage {

  private Logger logger = LogManager.getRootLogger();

  @FindBy(xpath = "//nav//*[@id='li_myaccount']/a")
  private WebElement dropDownMenu;

  @FindBy(xpath = "//nav//*[@id='li_myaccount']/ul/li/a")
  private WebElement dropDownMenuOptions;

  public HomePage() {
    PageFactory.initElements(DriverManager.getWebDriver(), this);
  }

  public void clickOnMyAccountDropMenuAndSelectOption(String option) {
    WaitForElement.waitUntilElementIsVisible(dropDownMenu);
    dropDownMenu.click();

    Thread.sleep(2000); // wait 2 seconds until dropdown loads
    List<WebElement> options = DriverManager.getWebDriver().findElements(By.xpath("//nav//*[@id='li_myaccount']/ul/li/a"));

    for(WebElement o: options) {
      if(o.getText().equals(option)) {
        o.click();
        return;
      }
    }
  }

  public void clickOnLoginLink() {
    WaitForElement.waitUntilElementIsClickable(loginLink);
    loginLink.click();
    logger.info("Clicked on Login link");
  }
}

您的xPaths不正确。我已经解决了。

答案 1 :(得分:0)

您需要更改一些定位符

xpath 上使用 css选择器始终是一个好习惯。

为此:

import { ReplaySubject } from "rxjs";
const { observable, reset, subject } = resettable(() => new ReplaySubject(3));
observable.subscribe(value => console.log(`a${value}`)); // a1, a2, a3, a4, a5, a6
subject.next(1);
subject.next(2);
subject.next(3);
subject.next(4);
observable.subscribe(value => console.log(`b${value}`)); // b2, b3, b4, b5, b6
reset();
observable.subscribe(value => console.log(`c${value}`)); // c5, c6
subject.next(5);
subject.next(6);

使用此:

  @FindBy(xpath = "//*[@id=\"li_myaccount\"]/ul")
   private WebElement dropDownMenu;  

为此:

@FindBy(css= "li#li_myaccount>a[aria-expanded]")
private WebElement dropDownMenu;  

使用此:

@FindBy(xpath = "//*[@id=\"li_myaccount\"]/ul/li")
private WebElement dropDownMenuOptions;  

在这种方法中,请像这样使用它:

@FindBy(css= "div#collapse ul.navbar-right li#li_myaccount li>a")
private WebElement dropDownMenuOptions;