How to print all the button texts within the url using Selenium through Java(如何通过 Java 使用 Selenium 打印 url 中的所有按钮文本)
本文介绍了如何通过 Java 使用 Selenium 打印 url 中的所有按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
步骤:
- 转到网站 > https://www.toolsqa.com/automation-practice-switch-windows/
- 从该页面获取按钮列表
- 打印页面上显示的按钮的名称.
代码试用:
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 方法
Instead of using By.tagName method I used By.cssSelector method
这是工作代码...
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);
}}}
以下是输出:
新的浏览器窗口
新消息窗口
新的浏览器标签
警报框
定时提醒
改变颜色
改变颜色
已禁用
这篇关于如何通过 Java 使用 Selenium 打印 url 中的所有按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:如何通过 Java 使用 Selenium 打印 url 中的所有按钮文本
基础教程推荐
猜你喜欢
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
