簡體   English   中英

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

[英]finding element by classname and tagname using selenium webdriver

有類和標記名,我正在編寫下面的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>

嘗試這樣的事情:

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

最好使用XPath或CSS選擇器而不是其他定位符,即className,id,name或tagName。 我也經歷過同樣的事情,即當我嘗試使用XPath時,我能夠正確定位元素並能夠點擊它。

您應該使用cssSelector而不是類定位器:

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

試試以下: -

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

String descriptionText = description.getText();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM