使用selenium webdriver通过classname和tagname查找元素

时间:2015-08-12 11:07:43

标签: java selenium

有类和标记名,我正在编写下面的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>

4 个答案:

答案 0 :(得分:3)

尝试这样的事情:

 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();

答案 1 :(得分:1)

最好使用XPath或CSS选择器而不是其他定位符,即className,id,name或tagName。 我也经历了同样的事情,即,当我尝试使用XPath时,我能够正确地找到元素并且能够点击它。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

请尝试以下方法: -

$mysqli = new mysqli("localhost","root","password","db");

$mysqli->query("SET NAMES 'utf8'");

$mysqli->query("SET CHARACTER SET utf8");

String descriptionText = description.getText();