硒使用nodejs单击一组等于元素的特定元素

时间:2019-10-04 20:04:51

标签: node.js selenium selenium-webdriver

我要单击的元素与其他元素具有相同的属性。.如何找到定位后要检查的元素,并检查其是否启用?

<html>
    <body>
        Input 1 <input type="tel" class="myInput">
        <br><br>
        Input 2 <input type="tel" class="myInput">
        <br><br>
        Input 3 <input type="tel" class="myInput">
        <br><br>
        Input 4 <input type="tel" class="myInput">
    </body>
</html>
require('chromedriver');
require('log-timestamp');
const webdriver = require('selenium-webdriver');
const chrome = require("selenium-webdriver/chrome");

var until = webdriver.until;
var By = webdriver.By;

async function myMain(){

  let driver = new webdriver.Builder().forBrowser('chrome').build();
  await driver.get('file:///C:/Users/myUser/Desktop/mypage.html');

  await driver.wait(until.elementLocated(By.className("myInput")),15000);
  le t btn = driver.findElement(By.className("myInput"));    
  await driver.wait(until.elementIsEnabled(btn ,15000));
  await driver.findElement(By.className("myInput")).click();
};
myMain();

2 个答案:

答案 0 :(得分:0)

如果DOM中有多个元素,则根据位置迭代elementResult数组

var elementResult = new Array();
elementResult=[];
elementResult = driverdriver.findElements(By.className("myInput"));

答案 1 :(得分:0)

如果要选择第4个输入,可以将xpath与索引4一起使用

//input[@class='myInput'][4]