简体   繁体   English

如何在Selenium Webdriver(Java)中找到包含特定文本的元素?

[英]How do I find an element that contains specific text in Selenium Webdriver (Java)?

Im trying to get id from element which have different id after each refresh and then print it 我试图从每个刷新后具有不同ID的元素获取id然后打印它

element code on website is: <div id="time_2019-03-28 07:36:50.604535">This element has a ID that changes every time the page is loaded</div> 网站上的元素代码是: <div id="time_2019-03-28 07:36:50.604535">This element has a ID that changes every time the page is loaded</div>

My code : 我的代码:

WebElement randomID = driver.findElement(By.xpath("//*[contains(@id, 'time')]"));
        System.out.println("Random ID is " + randomID.getAttribute("id"));

error:Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(@id, 'time')]"} 错误:线程“main”中的异常org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“// * [contains(@id,'time) “)]“}

Edit: Full HTML: 编辑:完整的HTML:

<html xmlns="http://www.w3.org/1999/xhtml"><head>
        <title>Selenium: Beginners Guide</title>
        <link type="text/css" rel="stylesheet" href="/styles/seleniumbeginners.css">
    <script src="chrome-extension://mooikfkahbdckldjjndioackbalphokd/assets/prompt.js"></script></head>
    <body>
        <div class="mainheading">Selenium: Beginners Guide</div>
        <div class="mainbody">
            <div><a href="/">Index</a></div>
            <div id="find">This item div has the id of find<br>
            put find into the target of Selenium IDE<br>
             and click the find button</div>
             <div id="divontheleft" class="leftdiv">
                <input id="but1" value="Button with ID" type="button">
                <br>
                <input value="Sibling Button" type="button">
             </div>
             <div id="divontheleft2"><input name="but2" value="Button with name" type="button"><br>
             <input id="random" type="button" value="Random"></div>
             <div id="divinthecenter" class="centerdiv"><input type="button" name="verifybutton" value="Verify this button
                 here"><br><input type="button" name="verifybutton1" value="chocolate"></div>
             <div id="time_2019-03-29 07:08:53.435150">This element has a ID that changes every time the page is loaded</div>
        </div>
        <script type="text/javascript">
            var randomButton;
            randomButton = document.getElementById('random');
            randomButton.addEventListener('click',function(e){
                but1 = document.getElementById('but1');
                but1.style.position = 'absolute';
                but1.style.top = 10 * Math.floor(Math.random() *  50);
                but1.style.left = 10 * Math.floor(Math.random() *  50); 
                }, true);
        </script>


</body></html>
@Test
    public void p0l0() throws InterruptedException {

        driver.get("file:\\C:\\Users\\pburgr\\Desktop\\p0l0.html");
        Thread.sleep(5000);

        List<WebElement> divsContainingId = driver.findElements(By.xpath("//div[contains(@id,'time')]"));

        String divsContainingIdSize = Integer.toString(divsContainingId.size());
        System.out.println(divsContainingIdSize);

        String id = divsContainingId.get(0).getAttribute("id");
        System.out.println(id);

    }

Gives me: 给我:

Starting ChromeDriver 2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5) on port 4982
Only local connections are allowed.
Bře 29, 2019 9:30:52 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
1
time_2019-03-29 07:08:53.435150

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中使用Selenium WebDriver查找特定的表元素? - How to find specific table element with selenium webdriver in java? 硒:如何查找带有特定文本的元素而没有包含选项 - Selenium: how to find element with with specific text without contains option 在Selenium WebDriver中,如何找到具有DOM ID但没有className的元素? - In Selenium WebDriver, how do I find an element with a DOM ID and no className? 如何使用Selenium Webdriver和Java从元素中获取文本? - How can I get text from element with Selenium webdriver and Java? 如何在selenium webdriver中为java索引元素 - How to find an Element by index in selenium webdriver for java 排序WebElement列表以查找特定的元素(Selenium WebDriver / Java) - Sorting a WebElement List to find specific Element (Selenium WebDriver/Java) 如何使用 Java 设置 Selenium WebDriver? - How do I setup Selenium WebDriver with Java? 如何获取/查找表中文本值的元素? Selenium Webdriver - How to get/find the element of text value in table? Selenium Webdriver 如何在Java中的Selenium Webdriver中的Webelements中单击特定元素 - How to click on specific element out of the webelements in selenium webdriver in java 如何在<span>Selenium中</span>查找具有特定文本的元素 - How to find a element with specific text in <span> with Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM