在 Selenium (Java) 中通过 cssSelector 获取元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27243184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:01:40  来源:igfitidea点击:

Get element by cssSelector in Selenium (Java)

javaselenium

提问by Sugan

<html>
    <body>    
        <div id="login-box" class="form-box">    
            <form id="frmlogin" class="form" name="frmlogin" method="post">
                <div class="body">
                    <div class="form-group">
                        <input id="email" class="form-control" type="text" maxlength="50"     value="[email protected]" placeholder="Email" name="email">
                        <span class="red">Please provide a valid email address</span>
                    </div>
                    <div class="form-group">
                        <input class="form-control" type="password" maxlength="15" placeholder="Password" name="password">
                        <span class="red">Password must not be empty</span>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>

I need to get "Please provide a valid email address" and "Password must not be empty" using nth-childin cssSelector.

我需要使用nth-childin获取“请提供有效的电子邮件地址”和“密码不能为空” cssSelector

I tried the below snippet:

我尝试了以下代码段:

//Case 2

//案例2

    driver.findElement(By.name("email")).clear();
    driver.findElement(By.name("email")).sendKeys("");
    String a=driver.findElement(By.cssSelector("form#frmlogin div.form-group:nth-child(1)>span")).getText();
    System.out.println(a);
    if(a.contains("valid email address"))
    {
        System.out.println("Login test case2 Passed");
    }
    else
    {
        System.out.println("Login test case2 Failed");
    }

It results in NoSuchElementFound.

它导致NoSuchElementFound.

回答by Murtaza Khursheed Hussain

You can use it by element selector

您可以通过元素选择器使用它

By.cssSelector("input[name=email]")

回答by Subh

Try these codes below:

试试下面的这些代码:

1- For returning 'Please provide a valid email address':

1- 返回“请提供有效的电子邮件地址”:

String a = driver.findElement(By.cssSelector("form#frmlogin div.form-group:nth-child(1)>span")).getText();
System.out.println(a);

2- For returning 'Password must not be empty':

2-返回“密码不能为空”:

String a = driver.findElement(By.cssSelector("form#frmlogin div.form-group:nth-child(2)>span")).getText();
System.out.println(a);

回答by Stefan

If you don't have to you could also use XPath selectors which are in my opinion easier to use when you have to select the nth element of something. Try this one:

如果您不需要,也可以使用 XPath 选择器,在我看来,当您必须选择某事物的第 n 个元素时,它更易于使用。试试这个:

For E-Mail:

对于电子邮件:

By.xpath("//div[@class='form-group'][1]/span")

For the password:

对于密码:

By.xpath("//div[@class='form-group'][2]/span")

But could you also provide the full HTML page you are working with? Maybe the form is inside an iframe and therefore Selenium has problems locating the element

但是您能否提供您正在使用的完整 HTML 页面?也许表单在 iframe 内,因此 Selenium 在定位元素时遇到问题

回答by Ariadne Pereira

Have you tried something like object.getAttribute("innerHTML"), literally with "innerHTML", like this ?

你有没有试过像这样object.getAttribute("innerHTML"),字面意思"innerHTML"是这样?

I had a similar problem getting a text from a span tag

我在从 span 标签获取文本时遇到了类似的问题

回答by Momo

You need to add .getText() method in order to get the text inside the tag.

您需要添加 .getText() 方法才能获取标签内的文本。