【问题标题】:How to store the value from the list to a variable in selenium webdriver如何将列表中的值存储到 selenium webdriver 中的变量中
【发布时间】:2019-12-03 12:33:41
【问题描述】:

我是 selenium webdriver 的新手。我想从 Ul 类中提取值并将其存储在变量中,但我无法这样做

这是我尝试过的 WebElement testuser = driver.findElement(By.cssSelector(".box ul.form li:nth-child(4)"));

div class="框" ul class="表格"

  • 用户类型
  • 学校管理员
  •         <li><h4>First Name</h4></li>
            <li>Faleata</li>
    

    说是找不到元素

    【问题讨论】:

    • 你想从ui中获取User TypeFirst Name的值吗?
    • 只有名字的值并将其存储到变量中。我基本上想通过检查整个列表来了解我们如何做。我完全检查列表中的值,最后提取它

    标签: selenium selenium-webdriver automation


    【解决方案1】:

    要处理动态元素,请使用WebDriverWait 并尝试以下xpath

    WebDriverWait wait = new WebDriverWait(driver, 30);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='box']/ul[@class='form']//li[./h4[text()='First Name']]/following::li[1]")));
    System.out.println(element.getText());
    

    【讨论】:

    • 谢谢你的帮助,我可以知道为什么“。”在h4之前使用过。
    • 在 li 中查找子 h4,其文本值为 First Name。
    • 如果我必须从列表中挑选并验证,您能否告诉我该怎么做。就像使用 List 一样,获取所有值并将其存储在列表中,然后验证以获取文本。
    • 你想验证什么值?
    • @paulbaboo : 我建议最好发布一个有新要求的新问题。这个社区不仅可以帮助自己,还应该帮助未来的读者。我希望能理解。
    【解决方案2】:

    你可以试试下面的代码

    WebDriverWait waitElmt = new WebDriverWait(driver, 60);
    WebElement element = waitElmt.until(ExpectedConditions.elementToBeClickable(By.xpath("(//ul[@class='form']//li)[4]")));
    String getText = element.getText();
    System.out.println(getText);
    

    【讨论】:

      【解决方案3】:

      你可以使用下面给出的代码sn-p

      WebDriverWait waitforelement = new WebDriverWait(driver, 30);
      
      string elementpath = "//div[@class='box']//child::ul[@class='form']//child::li[4]";
      
      WebElement firstname = wait.until(ExpectedConditions.elementToBeClickable
      (By.xpath(elementpath)));
      
      System.out.println(firstname.getText());
      

      【讨论】:

        【解决方案4】:

        我更喜欢使用selenium.support 包,有很多不错的功能

        就这么简单:

            IWebElement selectWebElement = filterColumns.FindElement(By.TagName("select"));
            SelectElement select = new SelectElement(selectWebElement);
            IList<IWebElement> optionsWebElements = select.Options;
        

        还有更多

        【讨论】:

          猜你喜欢
          • 2020-05-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-11
          • 2016-06-14
          • 1970-01-01
          • 2015-03-28
          相关资源
          最近更新 更多