【问题标题】:Button class name are same for 2 buttons how to identify the elements2个按钮的按钮类名称相同如何识别元素
【发布时间】:2021-06-17 14:15:29
【问题描述】:

【问题讨论】:

    标签: selenium selenium-webdriver


    【解决方案1】:

    如果该类确实是识别这些元素的最佳方法,我想您最好通过使用该类获取两个元素并以编程方式区分它们来做到这一点。我正在编写伪代码,因为您没有指定编码语言:

    webelement array buttons = driver.findElementsByCss('button.ss-button.ss-button.ss-button--secondary')
    firstButton = buttons[0]
    secondButton = buttons[1]
    

    【讨论】:

      【解决方案2】:

      xpath

      for first button: (//input[@class='your-class-name')[1]
      for second button: (//input[@class='your-class-name')[2]
      

      css 选择器

      for first button: input[class='your-class-name']:nth-child(1)
      for second button : input[class='your-class-name']:nth-child(2)
      

      Java 代码:

      WebElement button1 = _driver.findElements(By.cssSelector(".your-class-name")).get(0);
      
      WebElement button2 = _driver.findElements(By.cssSelector(".your-class-name")).get(1); 
      

      【讨论】:

      • 两个按钮名称相同,所以它不起作用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      相关资源
      最近更新 更多