java获取打印按钮,如何通过Java使用Selenium在url中打印所有按钮文本

Steps:

Get a list of buttons from that page

Print the name of the buttons that are displayed on the page.

Code trial:

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 buttons = driver.findElements(By.tagName("button"));

for ( int i=0; i

WebElement button = buttons.get(i);

if(button.isEnabled()){

System.out.println(buttons);

}}}}

解决方案

Instead of using By.tagName method I used By.cssSelector method

here is the Working code...

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 l1 = new ArrayList();

// WebElement b1 = driver.findElement(By.id("button1"));

// l1.add(b1.getText());

java.util.List 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);

}}}

Following is the Output:

New Browser Window

New Message Window

New Browser Tab

Alert Box

Timing Alert

Change Color

Change Color

Disabled

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值