我试图在"类别"中选择元素。在this page上逐一进行:
WebElement category = new WebDriverWait(driver, 10)
.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("tr[__gwt_row='" + rowIndex + "'] > td > div")))
.get(3);
category.click();
category.findElement(By.tagName("select")).sendKeys("Businesses");
category.click();
category.findElement(By.tagName("select")).sendKeys("Contacts");
抛出这个:"陈旧元素引用:元素未附加到页面文档"
也许存在另一种方法吗?
UPD: 如果我这样做,它可以工作:
WebElement categoryBusinesses = new WebDriverWait(driver, 10)
.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("tr[__gwt_row='" + rowIndex + "'] > td > div")))
.get(3);
categoryBusinesses.findElement(By.tagName("select")).sendKeys("Businesses");
WebElement categoryContacts = new WebDriverWait(driver, 10)
.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("tr[__gwt_row='" + rowIndex + "'] > td > div")))
.get(3);
categoryContacts.findElement(By.tagName("select")).sendKeys("Contacts");
但这是正确的方法吗?
UPD2: 我也试过这个:
WebElement element = categoryBusinesses.findElement(By.tagName("select"));
Select category = new Select(element);
category.selectByIndex(0);
category.selectByIndex(1);
但它不起作用。
答案 0 :(得分:1)
这真的很烦人......问题是每次更改SELECT的值时,页面都会重新创建元素,从而导致引用变得陈旧。基本上每次与它交互时都必须重新获取它。
我通常用这样的东西来制作函数,以便它们可以重复使用。
public static void GetCategoriesFromRow(int row)
{
By locator = By.cssSelector("table[__gwtcellbasedwidgetimpldispatchingfocus] tr[__gwt_row='" + row + "'] select");
for (int i = 0; i < new Select(driver.findElement(locator)).getOptions().size(); i++)
{
new Select(driver.findElement(locator)).selectByIndex(i);
System.out.println(new Select(driver.findElement(locator)).getFirstSelectedOption().getText());
}
}
你称之为
driver.get("http://samples.gwtproject.org/samples/Showcase/Showcase.html#!CwCellTable");
new WebDriverWait(driver, 10)
.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table[__gwtcellbasedwidgetimpldispatchingfocus]")));
GetCategoriesFromRow(0);
答案 1 :(得分:0)
试试这个:
WebElement element = categoryBusinesses.findElement(By.xpath("//*[@id="gwt-debug-contentPanel"]/div[2]/div/div[2]/div/div[3]/div/div/div/table/tbody/tr[1]/td/table/tbody[1]/tr[1]/td[4]/div/select"));
Select category = new Select(element);
category.selectByIndex(0);
category.selectByIndex(1);
这应该有用。