如何使用Selenium WebDriver

时间:2018-06-18 16:43:18

标签: selenium selenium-webdriver xpath

我正在尝试找到一种获取div值的方法,但我无法通过Xpath或cssSelector找到该元素。在这两种情况下(Xpath和cssSelector),Eclipse控制台都会显示错误消息:

  

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“// b [text()='ProtocoloÚnico'] / / :跨度1“}

使用xpath,我可以找到我需要的值但是,我无法得到它并在变量中播放。下面是包含页面HTML和我创建的xpath的图像。

Xpath 我创建了一个名为 verifyTextAssert 的方法,它采用 By 参数并返回文本。以下方法如下:

public String verifyTextAssert(By by) {

    return driver.findElement(by).getText();

}

TratOsFatRoamPage 类中,我创建了 getNuPu 方法,该方法调用 verifyTextAssert 方法并将xpath传递给此方法并返回找到的值。

private String obterNuPu() {

    return verifyTextAssert(By.xpath("//b[text()='Protocolo Único ']/following::span[1]")); 

}

enter image description here

<fieldset style="margin-bottom:10px;">
    <div id="content_titulo">Informações Gerais</div>
    <div style="padding-top:10px; padding-bottom: 10px; padding-left:5px; height:150%;">
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">ID Atendimento </b> <br> <span>5574474205</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Protocolo Único </b> <br> <span>2018227017094</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"><b class="label_tratamento ">Data Abertura </b> <br> <span>21/03/2018</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Hora Abertura </b> <br> <span></span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"><b class="label_tratamento ">Nome do Cliente </b> <br> <span></span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Tipo Cliente </b> <br> <span></span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Motivo 1 </b> <br> <span>Reclamação</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Motivo 2 </b> <br> <span>Claro Conta</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Motivo 3 </b> <br> <span>Contestação</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Motivo 4 </b> <br> <span>Serviços</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Motivo 5 </b> <br> <span>Pacote de Internet</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Worklist </b> <br> <span>BackOfficeAjusteRoaming GE</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">CNPJ </b> <br> <span>19988702272</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">MSISDN </b> <br> <span>69993932002</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">MSISDN2 </b> <br> <span></span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Status PS8 </b> <br> <span>Pendente</span></div>
        <div style="float:left; width:24%; padding-top:5px; padding-bottom: 8px; padding-left:5px; height:30px; font-size:x-small;"> <b class="label_tratamento ">Quem Abriu </b> <br> <span></span></div><input type="hidden" name="Id_Estrategia_Logado" id="Id_Estrategia_Logado" value="359"><input type="hidden" name="Id_Atividade_Logado" id="Id_Atividade_Logado" value="0">

    </div>
</fieldset>

2 个答案:

答案 0 :(得分:0)

您可以使用以下CSS选择器来获取文本。

 driver.FindElement(By.CssSelector("body > fieldset:nth-child(1) > div:nth-child(2) > div:nth-child(2) > span:nth-child(3)"));

修改

var element =  driver.FindElement(By.CssSelector("fieldset:nth-child(1) > div:nth-child(2) > div:nth-child(2) > span"));
string v = element.Text;

答案 1 :(得分:0)

您的overflow-y: scroll似乎是正确的。一个建议是尝试查找您的网页中是否xPath。如果是,请查看您的元素是否位于其中一个元素内。第二个建议是看看元素css属性,如果没有&#34;奇怪的&#34;属性