如何使用 Java 使用 ClassName 及其 Value 来识别元素

发布于 2025-01-16 06:22:42 字数 202 浏览 1 评论 0 原文

HTML:

<div class="MuiGrid-root MuiGrid-grid-xs-12 karutaNameNam css-1uqhpru">Super Heros</div>

如何使用 class = karutaNameNam 及其 Value =“Super Heros”来选择元素。

HTML:

<div class="MuiGrid-root MuiGrid-grid-xs-12 karutaNameNam css-1uqhpru">Super Heros</div>

How to select the element using class = karutaNameNam and its Value = "Super Heros".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2025-01-23 06:22:42

要将带有文本的元素标识为超级英雄,您可以使用以下任一定位器策略

  • cssSelector

    WebElement element = driver.findElement(By.cssSelector("div[class*='MuiGrid-root'][class*='karutaNameNam']"));
    
  • xpath

    WebElement element = driver.findElement(By.xpath("//div[contains(@class, 'karutaNameNam') and text()='超级英雄']"));
    

理想情况下,可以识别为 WebDriverWait 所需的元素https://stackoverflow.com/a/49749804/7429447">visibilityOfElementLocated 并且您可以使用以下任一定位器策略

  • cssSelector

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class*='MuiGrid-root'][class*='karutaNameNam']][value ='emptyAssembly']")));
    
  • xpath

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@class, 'karutaNameNam') and text()='超级英雄' ]")));
    

To identify the element with text as Super Heros you can use either of the following locator strategies:

  • cssSelector:

    WebElement element = driver.findElement(By.cssSelector("div[class*='MuiGrid-root'][class*='karutaNameNam']"));
    
  • xpath:

    WebElement element = driver.findElement(By.xpath("//div[contains(@class, 'karutaNameNam') and text()='Super Heros']"));
    

Ideally to identify the element you need to induce WebDriverWait for the visibilityOfElementLocated and you can use either of the following locator strategies:

  • cssSelector:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class*='MuiGrid-root'][class*='karutaNameNam']][value='emptyAssembly']")));
    
  • xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@class, 'karutaNameNam') and text()='Super Heros']")));
    
眼泪淡了忧伤 2025-01-23 06:22:42

我们可以在“cssSelector”中的类名之前使用点[.],以便在“findElement”方法中使用 className

WebElement element = driver.findElement(By.cssSelector('.MuiGrid-root.MuiGrid-grid-xs-12.karutaNameNam.css-1uqhpru'));

WebElement element = driver.findElement(By.cssSelector('.karutaNameNam'));

We can use dot[.] before class name inside 'cssSelector' for using className inside 'findElement' method

WebElement element = driver.findElement(By.cssSelector('.MuiGrid-root.MuiGrid-grid-xs-12.karutaNameNam.css-1uqhpru'));

WebElement element = driver.findElement(By.cssSelector('.karutaNameNam'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文