鼠标在Java Selenium中移动后单击元素

时间:2018-07-02 13:36:41

标签: java selenium

<a class="menu-item js-add-to-basket pizzaAddDefault js-change-ingredients js-add-pizza-to-basket" id="ph-add-to-basket-61504822" data-id="61504822" upsell-id="" href="/amrest-pizzahut/products#category/pizza" style="height: auto;">
            <div class="ph-order-on-preview">
                <button class="btn btn--primary">Zamów online</button>
            </div>
            <div id="ph-add-to-basket-61504822" class="box-counter ph-product-in-basket hidden" data-id="61504822">
                <span class="ph-products-in-basket-number">0</span>
            </div>
            <div class="menu-item__content">
                <h5>
                    <!-- 
                            <div class="ph-menu-favorite js-favourite" sec:authorize="!isAnonymous()">
                                <img th:src="@{/images/favourite-unchecked.png}" class="unchecked" alt="Nie ulubiona" />
                            </div>
                     -->
                    VEGE DELUX
                </h5>
                <p>
                    37,99 PLN
                </p>
                <p class="menu-item__description"></p>
            </div>
            <div class="menu-item__image">
                <img src="https://ocs-pl.oktawave.com/v1/AUTH_876e5729-f8dd-45dd-908f-35d8bb716177/amrest-web-ordering/GRD4/GRD4590/Smaki%20Premium/Pizza_vege_medium_450x450.png" alt="VEGE DELUX">
            </div>
        </a>

上面有我网站上的代码,我需要在此类上移动鼠标按钮,然后按“zamów在线”按钮

我对此物品有以下定位器:

    @FindBy (css="#ph-add-to-basket-61504822") WebElement firstpizza;
    @FindBy (css="#ph-add-to-basket-61504822 .ph-order-on-preview button") WebElement pizzabutton;

和功能

public void clickPizzaBasket() {
    Actions action = new Actions(driver);
    action.moveToElement(firstpizza);
    System.out.print("moved");
    pizzabutton.click();
}

执行此功能,我得到org.openqa.selenium.ElementNotVisibleException:元素在pizzabutton.click()行上不可见

我应以哪种方式访问​​“在线在线Zamów”按钮? 我也有以下网站的屏幕:http://oi68.tinypic.com/24qp8j8.jpg

3 个答案:

答案 0 :(得分:1)

这个怎么样-

 @FindBy (id="ph-add-to-basket-61504822") WebElement firstpizza;
 @FindBy (xpath="//button[contains(text(), 'online')]") WebElement pizzabutton;

    public void clickPizzaBasket() {
        Actions action = new Actions(driver);
        action.moveToElement(firstpizza).perform(); 
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(pizzabutton));
        pizzabutton.click();
    }

答案 1 :(得分:0)

尝试一下:

@FindBy (css="#ph-add-to-basket-61504822") WebElement firstpizza;
@FindBy (css="#ph-add-to-basket-61504822 > div.ph-order-on-preview > button") WebElement pizzabutton;

public void clickPizzaBasket() {
    Thread.sleep(2000); // add pause
    Actions action = new Actions(driver);
    action.moveToElement(firstpizza).perform(); // you forgot to perform the action
    System.out.print("moved");
    Thread.sleep(2000); // add pause
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(pizzabutton));
    pizzabutton.click();
}

答案 2 :(得分:0)

在页面上,@ FindBy中有多个ID为id的元素(css = 1和css = 2版本) 如果我使用List而不是WebElement可以正常工作

    @FindBy (css=".pizza.show h5") List <WebElement> namesOfPizzas;
    @FindBy (css=".pizza.show button") List <WebElement> pizzaOrderButtons;

和功能:

    public void clickOrderFirstPizza() throws InterruptedException {
    Actions action = new Actions(driver);
    Thread.sleep(500);
    action.moveToElement(namesOfPizzas.get(0)).build().perform();       
    System.out.print("moved");
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(pizzaOrderButtons.get(0)));
    pizzaOrderButtons.get(0).click();
}

感谢帮助