I am trying to return the text value, "My account" which is also a link.
I need to check the string to to prove i have logged into a website or not.
I have tried all the usual methods (xpath, by css , by href) but without success.. I am clearly doing something wrong.
can someone help me out with this ?
Hopefully the html image will show what i am trying to achieve. (click on code to display)
here is the code i am using to log into a site, when i login, the text "My account" appears as a href. I want to then return the href text to write to a spreadsheet.
See code snippet and some of the methods i have tried to return the href text string (i have tried many more .. but deleted them now as the didnt work)
Also see the result code.
WebElement element_search1 = driver.findElement(By.xpath("//*[@id='login']"));
driver.findElement(By.xpath("//*[@id='login']")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.titleContains("Login - mysite.com"));;
WebElement element_search2 = driver.findElement(By.xpath("//*[@id='subscriber_email']"));
driver.findElement(By.xpath("//*[@id='subscriber_email']"));
element_search2.clear();
element_search2.sendKeys("email@email.com");
element_search2.sendKeys(Keys.TAB);
// find and enter password
WebElement element_search3 = driver.findElement(By.xpath("//*[@id='subscriber_password']"));
driver.findElement(By.xpath("//*[@id='subscriber_password']"));
element_search3.clear();
element_search3.sendKeys("password");
driver.findElement(By.name("commit")).click();
System.out.println(driver.findElement(By.linkText("My account")).getAttribute("class"));
System.out.println(driver.findElement(By.linkText("My account")).getText());
Result
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"My account"}
it fails on : System.out.println(driver.findElement(By.linkText("My account")).getAttribute("class"))
Here is the Answer to your Question:
To retrive the text value of a class "user-info" ie My account
you can use the following lines of code:
String my_account = driver.findElement(By.xpath("//nav[@class='main-top-nav']//a[@class='user-info' and contains(@href,'/home/manage_account')]")).getAttribute("innerHTML");
System.out.println(my_account);
Let me know if this Answers your Question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.