【发布时间】:2016-08-26 09:08:28
【问题描述】:
尽管 x-path 选择了元素,并且我尝试使用 java executor,但是在代码运行时,它只能单击购物车中第一个元素的更新按钮。以下是代码:
在将 3 个产品放入购物车后,我已将文本框添加到列表中:
我已附上图片: 网址="http://live.guru99.com/"
List<WebElement> li2 =driver.findElements(By.xpath(".//td[@class='product-
cart-actions']/input"));
for(int j=0;j<li2.size();j++)
{
if(j==0)
{
li2.get(j).clear();
li2.get(j).sendKeys("4");
driver.findElement(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update']")).click();
li2 =driver.findElements(By.xpath(".//td[@class='product-cart-actions']/input"));
}
else if(j==1)
{
li2.get(j).clear();
li2.get(j).sendKeys("2");
//wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update' and @type='submit']")));
//driver.findElement(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update' and @type='submit']")).isDisplayed();
WebElement element = driver.findElement(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update' and @type='submit']"));
try {
if (element.isEnabled() && element.isDisplayed()) {
System.out.println("Clicking on element with using java script click");
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
} else {
System.out.println("Unable to click on element");
}
} catch (StaleElementReferenceException e) {
System.out.println("Element is not attached to the page document "+ e.getStackTrace());
} catch (NoSuchElementException e) {
System.out.println("Element was not found in DOM "+ e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to click on element "+ e.getStackTrace());
}
// if (element.isDisplayed()) {
// element.click();
//}
li2 =driver.findElements(By.xpath(".//td[@class='product-cart-actions']/input"));
}
else
{
li2.get(j).clear();
li2.get(j).sendKeys("3");
WebElement element1 = driver.findElement(By.xpath(".//td[@class='product-cart-actions']/button[@title='Update']"));
if (element1.isDisplayed()) {
element1.click();
}
}
【问题讨论】:
-
@guy:你能看看这个吗?
标签: java selenium selenium-webdriver automation selenium-chromedriver