Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过Xpath提取登录用户的文本_Java_Selenium_Selenium Webdriver_Xpath_Webdriver - Fatal编程技术网

Java 如何通过Xpath提取登录用户的文本

Java 如何通过Xpath提取登录用户的文本,java,selenium,selenium-webdriver,xpath,webdriver,Java,Selenium,Selenium Webdriver,Xpath,Webdriver,登录后,会出现欢迎“用户名”消息 我想编写xpath来捕获该用户。我尝试过一些想法: //span[@id='WhoLoggedInUpdatePanel']//h1//br[contains(text(),'Welcome Sheikh')] //h1[contains(text(),'"Welcome Sheikh"')] 这些都不起作用。你的建议会很有帮助 尝试下面的方法获取文本 String user = driver.findElement(By.xpath("//span[@i

登录后,会出现欢迎“用户名”消息

我想编写xpath来捕获该用户。我尝试过一些想法:

//span[@id='WhoLoggedInUpdatePanel']//h1//br[contains(text(),'Welcome Sheikh')]

//h1[contains(text(),'"Welcome Sheikh"')]

这些都不起作用。你的建议会很有帮助

尝试下面的方法获取文本

String user = driver.findElement(By.xpath("//span[@id='WhoLoggedInUpdatePanel']/h1").getAtrribute("innerText");

尝试下面的一个来获取文本

String user = driver.findElement(By.xpath("//span[@id='WhoLoggedInUpdatePanel']/h1").getAtrribute("innerText");

使用名称进行定位不好,因为如果登录凭据发生更改,则名称将更改

使用
//span[@id='whoggedinupdatepanel']/h1
并抓取
或等效CSS选择器
下的文本span[id='whoggedinupdatepanel']>h1

String userName = driver.findElement(By.xpath("//span[@id='WhoLoggedInUpdatePanel']/h1")).getText();
然后验证您的使用是否符合预期

if(userName.contains("Sheikh")){
    System.out.println("valid user");
} else {
    System.out.println("invalid user");
}

使用名称进行定位不好,因为如果登录凭据发生更改,则名称将更改

使用
//span[@id='whoggedinupdatepanel']/h1
并抓取
或等效CSS选择器
下的文本span[id='whoggedinupdatepanel']>h1

String userName = driver.findElement(By.xpath("//span[@id='WhoLoggedInUpdatePanel']/h1")).getText();
然后验证您的使用是否符合预期

if(userName.contains("Sheikh")){
    System.out.println("valid user");
} else {
    System.out.println("invalid user");
}

要提取文本Welcome Sheikh,因为它是其祖先
节点中的文本节点,您需要使用executeScript()方法,并且可以使用以下解决方案:

WebElement myElement = driver.findElement(By.xpath("///span[@id='WhoLoggedInUpdatePanel']/h1"));
String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].lastChild.textContent;', myElement).toString();

要提取文本Welcome Sheikh,因为它是其祖先
节点中的文本节点,您需要使用executeScript()方法,并且可以使用以下解决方案:

WebElement myElement = driver.findElement(By.xpath("///span[@id='WhoLoggedInUpdatePanel']/h1"));
String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].lastChild.textContent;', myElement).toString();

您的示例中没有包含文本“Welcome Sheikh”-单词之间有很多空格字符!查看normalize space函数-例如(另外,
标记不是“正确的”html,因为它是一个未关闭的标记-应该是“
”-这也意味着它不包含任何文本)是否要提取文本或按文本定位?示例中没有包含文本“Welcome Sheikh”-单词之间有很多空格字符!查看normalize space函数-例如(另外,
标记不是“正确的”html,因为它是一个未关闭的标记-应该是“
”-这也意味着它不包含任何文本)是否要提取文本或按文本定位?CSS选择器应该是
#whoggedinupdatepanel>h1
。它更简单、更短,并且可以完成相同的任务。您的CSS选择器应该是
#WhoLoggedInUpdatePanel>h1
。它更简单,更短,并且完成同样的事情。