java无法在selenium的列表中存储通过搜索文本字段生成的下拉列表项
我通过youtube搜索文本字段中的发送键发送了一个可搜索的关键字。但当下拉列表出现在搜索文本字段下方时,我无法在列表中存储下拉列表项并单击其中任何一个。由于打印列表大小,我得到“0”
package SomeBasicAutomationPractice;
import java.util.List;
import org.apache.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Practice_dynamic_xpath {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "G:\\VivekAutomationPractice\\src\\drivers\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.youtube.com/");
driver.manage().window().maximize();
Thread.sleep(5000);
driver.findElement(By.xpath("//input[@id='search']")).sendKeys("selenium");
List<WebElement> li=driver.findElements(By.xpath("//*[starts-with(@id,'sbse')]"));
System.out.println(li.size());
li.get(2).click();
}
}
# 1 楼答案
你能试试这个吗 驾驶员findElements(通过.cssSelector(#results ol#search results>;li h3>;a))
# 2 楼答案
请尝试下面的代码,我的朋友。如果此代码对您有帮助,那么我请求您将其标记为已接受。这就是Stackoverflow的工作原理我的朋友:)