I need to get the text of a td element with selenium. I have problem with extract text, I just receive null. I tried used list, getText() and so on. The HTML code is on the picture and element looks like you can see on the picture.
getDriver().findElement(By.xpath("//*[#id=\"standortTable\"]/tbody/tr/td[2]")).isDisplayed();
String test = getDriver().findElement(By.xpath("//*[#id=\"standortTable\"]/tbody/tr/td[2]")).getText();
System.out.println(test);
But I receive NULL, just "".
I guess you are missing a wait I.e. you trying to read the element text content before it is completely rendered.
Try this:
WebDriverWait wait = new WebDriverWait(getDriver(), 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[#id='standortTable']//td[#aria-colindex='2']")));
String test = getDriver().findElement(By.xpath("//table[#id='standortTable']//td[#aria-colindex='2']")).getText();
System.out.println(test);
The element is an Angular element, so to extract the text Teststandort1 you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using xpath and getText():
System.out.println(new WebDriverWait(getDriver(), 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[#id='standortTable']//tbody//tr[#class='ng-star-inserted']//td[#aria-colindex='2']"))).getText());
Using cssSelector and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(getDriver(), 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table#standortTable tbody tr.ng-star-inserted td[aria-colindex='2']"))).getAttribute("innerHTML"));
Related
I have been trying to send a text to a text field that has the changing element id=PolarisTextField83 each time a log into a page (PolarisTextField## keeps changing its value like id=PolarisTextField45) as I have found that the id element is dynamic and the only static and unique part of the HTML is the placeholder example text which is placeholder="e.g. Shirts".
Therefore, I wonder if there is a way of locating placeholder="e.g. Shirts" then sending text to its respective type field (PolarisTextField##)?
I have tried to use
driver.findElementsByTagName("e.g. Shirts").sendKeys("test text");
but came to the understanding that I cannot follow .sendKeys() after findElementsByTagName.
I am new to both java and selenium and would appreciate any help and/or guidance!!
You can find the element by placeholder value using XPath
driver.findElement(By.xpath("//*[#placeholder='e.g. Shirts']")).sendKeys("test text");
Try below xpath to deal with dynamic web element.
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.findElement(By.xpath("//*[starts-with(#id,'PolarisTextField')]")).sendKeys("Test");
To send a character sequence within the element you can use either of the following Locator Strategies:
Using css_selector, id and placeholder attributes:
driver.findElement(By.cssSelector("[id^='PolarisTextField'][placeholder$='Shirts']")).sendKeys("test text");
Using xpath, id and placeholder attributes:
driver.findElement(By.xpath("//*[starts-with(#id, 'Shirts') and contains(#placeholder, 'Shirts')]")).sendKeys("test text");
Ideally, to send a character sequence within the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("[id^='PolarisTextField'][placeholder$='Shirts']"))).sendKeys("test text");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[starts-with(#id, 'Shirts') and contains(#placeholder, 'Shirts')]"))).sendKeys("test text");
I am trying to click on a hyperlink with a space using Java and Selenium. Here is a sample of the code
<h3 class="side menu">
<a class="side-menu" href="/configurations">
<span class="menu-icon ca"></span>Configuration
</a>
</h3>
I have tried using xpath starts-with and contains with no luck.
driver.findElement(By.xpath("//a[starts-with(text(),’Configuration’)]")).click();
To handle dynamic element use WebDriverWait and elementToBeClickable with expected conditions.Use the following Xpath locator strategies.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[#class="side-menu"][normalize-space(.)="Configuration"]')));
element.click()
OR
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[#class="side-menu"][contains(.,"Configuration")]')));
element.click()
I think all you need is to add a forward slash:
driver.findElement(By.xpath("//a[starts-with(text(),'/configuration')]")).click();
It works for me in the Chrome console if I type:
$x("//a[starts-with(#href,'/configuration')]")
I would use normalize-space when writing the xpath for the text, which will trim the leading and preceding white chars (spaces/tabs/newlines)
Here is how you should do it in your question.
//a[normalize-space(.)='Configuration']
Tested the xpath in chrome console for click() successfully using javascript.
Task:
search FAA in search box :
I have tried this:-
webdriver.select_tabs(search.btnSearch);
Thread.sleep(3000);
WebElement searchbox = driver.findElement(By.id("search-text"));
Actions builder = new Actions(driver);
Actions seriesOfActions = builder.moveToElement(searchbox).click().sendKeys(searchbox, "FAA");
seriesOfActions.perform();
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"search-text\"]")));
element.sendKeys("FAA");
element.sendKeys(Keys.ENTER);
webdriver.enter_key(search.txtSearch, Keys.ENTER);
webdriver.enter_Text(search.txtSearch, "FAA");
webdriver.enter_key(search.txtSearch, Keys.ENTER);
Got this error:-
org.openqa.selenium.ElementNotVisibleException: element not visible
Use below xpath :
(//input[#id='search-text'])[2]
and use like :
driver.findElement(By.xpath("(//input[#id='search-text'])[2]")).sendKeys("FAA");
When you find by this id in console it is giving two elements and first one is not visible but second one is the actual input box.
By definition, Selenium interacts with the browser like a real user would. A real user would not be able to type into a textbox/editbox which is hidden. Either you need to change the visibility of the input, re-evaluate why you need to interact with a hidden element, or use javascript executor to set the value of the input, something like this:
driver.executeScript("arguments[0].value='" + textToEnter + "'", element);
To send a character sequence to the search field on the website https://faatoday.com/ you need to induce WebDriverwait to wait for the search icon to be clickable and then again induce WebDriverWait once again for the desired element to be clickable and then send the character sequence as follows:
Code Block:
driver.get("https://faatoday.com/");
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#navbarNav span.sicon-holder.fabutton#searchicon>i.fa.ssearch.fa-search.fa-lg#sicons"))).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='navbarNav']//input[#class='search-text form-control mr-sm-2' and #id='search-text']"))).sendKeys("FAA");
Browser Snapshot:
xpath:
//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()
The above code only returns a webelement am not able to print the text.
WebElement txt= driver.findElement(By.xpath(
"//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()"));
System.out.println(txt);
HTML:
12.6
caught Hughes
73/4
this should work :
WebElement txt= driver.findElement(By.xpath("//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]"));
System.out.println(txt.getText());
Selenium webdriver return all test under element object including child element.
So after reading you should remove integer values from text.
Thanks
Sadik
I want to getText() using By.id or By.cssSelector.
I managed to solve my problem by doing getAttribute("value"), but I don't understand why getText() doesn't work like I expect it, and I might need it so all help is appreciated.
Here is the Java code:
WebDriverWait wait = new WebDriverWait(driver, 10);
Boolean elementIsPresent = wait.until(ExpectedConditions.textToBePresentInElementValue(By.cssSelector("#general_service_name"),"[reg] general_service_name")); // true
//WebElement general_service_name = driver.findElement(By.cssSelector("#general_service_name"));
WebElement general_service_name = driver.findElement(By.id("general_service_name"));
// Display check
Boolean isDisplayed;
if(general_service_name.isDisplayed()) isDisplayed = new Boolean(true); else isDisplayed = false; //true
String text_empty = general_service_name.getText(); //""
String text_with_value = driver.findElement(By.id("general_service_name")).getAttribute("value"); //"[reg] general_service_name"
And HTML:
<input id="general_service_name" type="text" value="[reg] title" name="general_service_name" style="float:left;"/>
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getText()
getText() delivers the innerText of a WebElement.
Your input field does not have any inner Text. The text is found inside your value-attribute, hence accessing it via getAttribute("value") is the correct way to do it.
Java
ele.getAttribute("innerHTML");
This could get the text already in the background and not displayed on the page yet.
Simple answer - it's designed this way. getText() parses the content of the tag (i.e. its innerText), which is obviously empty for inputs.
You may use this if you want to search for a given text on a WebElement. Pass it directly or through a string:
String textoToSearch = "Text inside Webelement";
driver.findElement(By.id("someID).getText().contains("textToSearch");
getText()
getText() returns the visible text of this element.
java.lang.String getText()
Get the visible (i.e. not hidden by CSS) text of this element, including sub-elements.
Returns:
The visible text of this element.
As per the HTML of the element:
<input id="general_service_name" type="text" value="[reg] title" name="general_service_name" style="float:left;"/>
The WebElement doesn't have a visible text but the value attribute have the value set as [reg] title.
So to extract the value of the value attribute i.e. [reg] title you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(wd.findElement(By.cssSelector("input#general_service_name[name='general_service_name']")).getAttribute("value"));
Using xpath:
System.out.println(wd.findElement(By.xpath("//input[#id='general_service_name' and #name='general_service_name']")).getAttribute("value"));
Ideally, you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector and getText():
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#general_service_name[name='general_service_name']"))).getAttribute("value"));
Using xpath and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#id='general_service_name' and #name='general_service_name']"))).getAttribute("value"));