java 使用 selenium webdriver 通过类名和标记名查找元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31963439/
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-11-02 19:25:33  来源:igfitidea点击:

finding element by classname and tagname using selenium webdriver

javaselenium

提问by Pankaj Sharma

There are classes and tagname and I am writing the below selenium code to find description from below code but its not working.

有类和标记名,我正在编写下面的 selenium 代码以从下面的代码中找到描述,但它不起作用。

WebElement WWdescription = driver.findElement(By.className("atb-delivery-accordions").className("section highlight-01").tagName("p"));    


<div class="atb-delivery-accordions">
    <div class="page-accordion opened">
    <input id="moreDetails-acc" class="acc-check" type="checkbox" checked="">
    <label class="acc-label opened" data-panel-id="moreDetailsAcc" for="moreDetails-acc">Description</label>
    <div class="content" data-panel-id="moreDetailsAcc" style="display: block;">
    <div class="information-panel">
    <div class="subcontent">
    <div class="section highlight-01">
    <p>A pretty floral lace collection combining contrast bows and trims for a feminine on trend look. The fuller coverage of our post surgery bras provide support, comfort and confidence. The dual cotton pockets are perfect for a prosthesis. Combine style and value with this pack of 2 bras.</p>

回答by Saritha G

Try something like this:

尝试这样的事情:

 WebElement parentEle = driver.findElement(By.xpath("//div[@class='atb-delivery-accordions']"));

 WebElement descriptionEle = parentEle.findElement(By.tagName("p"));

 //Get the text from the description element.
 String descriptionText = descriptionEle.getText();

回答by Kiran Angara

It is better to use XPath or CSS selector instead of other locaters i.e., className, id, name or tagName. I also experienced the same i.e., when I tried with XPath I was able to locate the element properly and was able to click on it.

最好使用 XPath 或 CSS 选择器而不是其他定位器,即 className、id、name 或 tagName。我也遇到过同样的情况,即,当我尝试使用 XPath 时,我能够正确定位元素并能够单击它。

回答by Slav Kurochkin

You should use cssSelector instead of class locator:

您应该使用 cssSelector 而不是类定位器:

    WebElement WWdescription = driver.findElemenet(By.cssSelector(".atb-delivery-accordions>div.section.highlight-01>p");

回答by Vishal Jagtap

Try below:-

试试下面:-

WebElement description = driver.findElement(By.xpath("//div[@class='atb-delivery-accordions']/p"));

String descriptionText=description.getText();

字符串 descriptionText=description.getText();