我正在尝试使用Java在Selenium Webdriver中自动购买电子商务网站(仅用于培训)。当我点击“添加到购物车”时,会出现一个弹出窗口,其中显示“继续购物”或“继续结帐”按钮。问题是,Selenium无法检测Checkout按钮。在进一步调查(isDisplayed())时,我发现它无法检测整个弹出窗口。我得到的是ElementNotVisibleException。
我尝试了以下选项: 1)检查是否有多个windowHandles,发现只有一个窗口句柄。 2)检查弹出窗口是否是另一帧。但它是主框架的一部分。因此也排除了切换到另一个框架。 3)我尝试向下滚动窗口一点。 4)尝试使用WebdriverWait来定位Element的存在。
我不确定我在这里缺少什么。任何帮助表示赞赏。
以下是我遇到问题的确切页面。 http://automationpractice.com/index.php?id_product=4&controller=product
我的代码片段:
WebElement proceed_to_checkout = (new WebDriverWait(driver, 20)).until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='layer_cart']/div[1]/div[2]/div[4]/a/span"))) ;
proceed_to_checkout.click();
答案 0 :(得分:0)
WebDriver wd = new FirefoxDriver();
wd.get("http://automationpractice.com/index.php?id_product=4&controller=product");
Thread.sleep(2000L);
wd.findElement(By.xpath(".//*[@id='add_to_cart']/button")).click();
Thread.sleep(2000L);
wd.findElement(By.xpath(".//*[@id='layer_cart']/div[1]/div[2]/div[4]/a/span")).click();
我可以使用此代码单击按钮。让我知道这是否适合您或您需要解释但我认为代码非常简单。
答案 1 :(得分:0)
这对我来说非常有效,没有任何打嗝:
package queries;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SQ34442686 {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.get("http://automationpractice.com/index.php?id_product=4&controller=product");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[text()='Add to cart']")).click();;
driver.findElement(By.xpath(".//a[contains(@title,'Proceed to checkout')]")).click();
driver.close();
}
}