public class CssSelector3 {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://qa.letslearnindia.com");
driver.manage().window().maximize();
driver.findElement(By.linkText("Sign in")).click();
Thread.sleep(5000);
driver.findElement(By.cssSelector("input[id='inputSuccess2']")).sendKeys("tester42@gmail.com");
driver.findElement(By.cssSelector("input[id='inputSuccess3']")).sendKeys("123456");
driver.findElement(By.cssSelector("input[id='btn_login']")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='navbar']/ul/li[2]/a")).click();
driver.findElement(By.xpath("//*[@id='horizontalTab']/div/div[1]/div[1]/div[2]/a/input")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='full_height_base']/div/div[3]/div[3]/div[2]/div/ul[2]/li[1]/a")).click();
driver.findElement(By.xpath("//*[@id='courseTitle']")).sendKeys("Automation Test");
driver.findElement(By.xpath("//*[@id='courseSubtitle']")).sendKeys("Automating the test cases");
Thread.sleep(5000);
WebElement dropdown = driver.findElement(By.xpath("//*[@id='validate-me-plz']/div[1]/div[2]/div/p/span"));
List<WebElement> li = dropdown.findElements(By.tagName("li"));
System.out.println(li.size());
String element;
for(int i =0; i<li.size();i++){
element = li.get(i).getAttribute("data-val");
if(element.equals("English")){
li.get(i). click();
答案 0 :(得分:0)
从<select>
代码中选择时,您应该使用Select class
WebElement dropdown = driver.findElement(By.id("courseLanguage")); // locate the dropdown
Select select = new Select(dropdown); // initialize select
select.selectByVisibleText("English"); // choose the option with "English" as text
// select.selectByValue("English"); // choose the option with "English" as value
它仍然会出现错误,因为“元素当前不可见,因此可能无法与”
进行交互
要确保在交互之前元素可见,请使用显式等待
// this will wait up to 10 seconds for the dropdown to be visible and will return the dropdown element
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("courseLanguage")));
Select select = new Select(dropdown);
select.selectByVisibleText("English");
答案 1 :(得分:0)
尝试使用findElements方法选择具有select标签的所有元素 然后将所需元素传递给Select类,如下所示:
List<WebElement> AllselectTags= driver.findElements(By.tagName("select"));
WebElement selectedElement = AllselectTags.get(0);
Select s = new Select(selectedElement);
s.selectByValue("English");