gpt4 book ai didi

java - 如何通过 Java 使用 Selenium 打印 url 中的所有按钮文本

转载 作者:行者123 更新时间:2023-12-02 09:50:00 24 4
gpt4 key购买 nike

步骤:

代码试用:

package com.practice;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Buttons {

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Oderint dum metuant\\eclipse-workspace\\JAR FILES\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.toolsqa.com/automation-practice-switch-windows/");

List <WebElement> buttons = driver.findElements(By.tagName("button"));
for ( int i=0; i<buttons.size();i++){
WebElement button = buttons.get(i);
if(button.isEnabled()){
System.out.println(buttons);
}}}}

最佳答案

我没有使用By.tagName方法,而是使用By.cssSelector方法

这是工作代码...

package stackOverflow;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ToolsqaCom {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "D:\\Tushar\\JARs\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://www.toolsqa.com/automation-practice-switch-windows");
driver.manage().window().maximize();

// ArrayList<String> l1 = new ArrayList<String>();
// WebElement b1 = driver.findElement(By.id("button1"));
// l1.add(b1.getText());

java.util.List<WebElement> b2 = driver.findElements(By.cssSelector("p button"));

for (int i = 0; i < b2.size() - 1; i++) {
String string = b2.get(i).getText();
System.out.println(string);

}}}

以下是输出:

New Browser Window

New Message Window

New Browser Tab

Alert Box

Timing Alert

Change Color

Change Color

Disabled

关于java - 如何通过 Java 使用 Selenium 打印 url 中的所有按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56388392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com