如何查找具有value属性的元素(在运行时创建)

时间:2014-04-22 14:11:40

标签: variables dynamic selenium-webdriver runtime.exec

我以前点击该元素的不同方式是,

driver.findElement(By.xpath(".//*[@value=.,"+NewStateName+"]")).click();
driver.findElement(By.cssSelector(".//*[@value=.,'"+NewStateName+"']")).click();
driver.findElement(By.value(NewStateName));
driver.findElement(By.linkText(NewStateName));
driver.findElement(By.xpath("//span[contains(.,"+NewStateName+")]"));

In above code 'NewStstaeName is a string variable for which the value is dynamically generated during run time.



Error returned:
org.openqa.selenium.InvalidSelectorException: The given selector .//*[@value=.,NEWSTATEABC21M] is either invalid or does not result in a WebElement. The following error occurred:

1 个答案:

答案 0 :(得分:0)

我认为你需要围绕NewStateName的值引用,试试这些:

driver.findElement(By.xpath(".//*[@value=.,'"+NewStateName+"']")).click();
driver.findElement(By.xpath("//span[contains(.,'"+NewStateName+"')]"));

结果选择器将如下所示:

  

.//* [@值=,' NEWSTATEABC21M']