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

在Java Selenium中鼠标移动后单击元素,java,selenium,Java,Selenium,和功能 @FindBy (css="#ph-add-to-basket-61504822") WebElement firstpizza; @FindBy (css="#ph-add-to-basket-61504822 .ph-order-on-preview button") WebElement pizzabutton; 执行此函数时,我得到org.openqa.selenium.ElementNotVisibleException:元素在pizzabutton上不可见。

和功能

    @FindBy (css="#ph-add-to-basket-61504822") WebElement firstpizza;
    @FindBy (css="#ph-add-to-basket-61504822 .ph-order-on-preview button") WebElement pizzabutton;
执行此函数时,我得到org.openqa.selenium.ElementNotVisibleException:元素在pizzabutton上不可见。单击()行

我应该以何种方式访问“zamów online”按钮

试试这个:

public void clickPizzaBasket() {
    Actions action = new Actions(driver);
    action.moveToElement(firstpizza);
    System.out.print("moved");
    pizzabutton.click();
}
这个怎么样-

@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();
}

在页面上,@FindBy中有多个id为的元素(css=1和css=2版本) 如果我使用列表而不是WebElement,它会起作用

 @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();
    }

感谢您对xpath:org.openqa.selenium.NoSuchElementException的新定位器的帮助

:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“span[包含(,'Zamów online')]”。如果我在我的操作中添加了perform-没有任何更改,仍然org.openqa.selenium.ElementNotVisibleException:element不可见您确定firstpizza id是静态的吗?是的,此定位器是静态的它是否打印“移动”然后引发异常?请再试一次我的xpath,我有一个错误。xpath已更新
 @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();
    }
    @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();
}