【发布时间】:2017-09-10 00:21:12
【问题描述】:
我无法选择没有关联属性的下拉菜单。在 HTML 代码下方,可以看到有一个 select 标签,但没有 id 或 name。如何在 Selenium 中选择该标签?
<!DOCTYPE html>
<html>
<body>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</body>
</html>
下面是该下拉菜单所在的网站。 网址:https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select
我已经尝试从文档的根目录,跟随兄弟等,但没有任何结果。 我得到 NOSUCELEMENT 异常
这就是原因,我在这里发布一个问题。
以下是我的代码,
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class X
{
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select");
WebElement element= driver.findElement(By.xpath("/html/body/select"));
Select s = new Select(element);
s.selectByValue("saab");
}
}
【问题讨论】:
-
需要先切换到
iframe才能找到元素。 -
先来frame再去select,“Saab”中的“S”应该是大写