我正在嘗試打印按鈕的文本,但是每次我的 for 循環(huán)運行時,我都沒有從按鈕獲取下一個文本,而是始終將第一個按鈕的文本打印到控制臺。任何人都可以幫助理解我在哪里犯了錯誤嗎?public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); driver.get("http://www.example.com/example.php"); for (int i = 1; i <= 4; i++) { System.out.println(driver.findElement(By.xpath("//div[@class='responsive- tabs responsive-tabs--enabled']/ul/li['+ i +']")).getText()); Thread.sleep(3000); }}以上是我的代碼,如果我嘗試單獨打印按鈕的文本而不是進(jìn)入 for 循環(huán),我可以打印,但是當(dāng)我在 for 循環(huán)中使用相同的代碼時,我只能從第一個按鈕獲得 4 次文本。我當(dāng)前的輸出: Appium Appium Appium Appium預(yù)期輸出 Appium Selenium API 測試
1 回答

夢里花落0921
TA貢獻(xiàn)1772條經(jīng)驗 獲得超6個贊
代替for 循環(huán),您可以像這樣為每個循環(huán)尋找:
請注意,我正在使用findElements, 以便我可以遍歷列表。
List<WebElement> elements = driver.findElements(By.xpath("//div[@class='responsive- tabs responsive-tabs--enabled']/ul/li"))
for(WebElement ele : elements){
System.out.println(ele.getText());
}
添加回答
舉報
0/150
提交
取消