reading text from textarea in webdriver - selenium

I am trying to read text from textarea when writing a webdriver test in Java. For some reason I am getting null back when I use .getAttribute():
WebElement text = wd.findElement(By.id("edit-pi-sample-geo-id"));
String textagain = text.getAttribute("aaaa");
How do I fix this?

I got this working. Here is the solution-
WebElement text = wd.findElement(By.id("edit-pi-analytics-tms-id"));
String textagain = text.getAttribute("value");
I was using the actual value in the textarea in the previous code example i posted which was kinda silly. Thanks guys for your help

I'm using selenium version 3.4 and using element.getAttribute("value") that work for me. The element.getText() would return empty value for TextArea.

So finding an element returns a web element. To get the text of the element you have to call getText(), so from above
WebElement element = wd.findElement(By.id("edit-pi-sample-geo-id"));
String text = element.getText()

Related

Issue with send_keys in Selenium

I am trying to fill up textbox using Selenium.
description=driver.find_element_by_xpath("//*[contains(text(), 'add
description')]")
description.click()
description = driver.find_element_by_xpath("//*[#type='text']")
description.send_keys(""+str(description))
For some reason the variable value of description isnt passed to the textbox but webelement id and session id which is :
<selenium.webdriver.remote.webelement.WebElement (session="f2943bc99f2fbbec90c3fa9c0cb68e20", element="0.3535547756904527-5")>
Can anyone help me on this?
try with this code :
description.send_keys("""Just a testing """)
OR
description.send_keys(str(description.text))
If I've understood your code correctly 'description' contains a web element. So your send_keys is essentially typing the web element in string form.
From the (unofficial) documentation:
find_element_by_xpath(xpath)
Finds an element by xpath.
Args: xpath - The xpath locator of the element to find
Returns: WebElement - the element if it was found
Raises: NoSuchElementException - if the element wasn’t found
Usage: element = driver.find_element_by_xpath(‘//div/td[1]’)
http://selenium-python.readthedocs.io/api.html

Selinium web driver- Unable to locate href

I trying to locate a link "Taschen" in the below code(<a data-flyout-target="handbags" title="Taschen" href="#">Taschen</a>) using selinium webdriver. Could anyone please guide me
<a data-flyout-target="handbags" title="Taschen" href="#">Taschen</a>
Here are your choices:
By.className
By.cssSelector
By.id
By.linkText
By.name
By.partialLinkText
By.tagName
By.xpath
So assuming that "Taschen" is partial link text
WebElement aLink = driver.findElement(By.partialLinkText("Taschen"));
If you haven't bothered with page loads and proper implicit waits then this might work better
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement aLink = wait.until(
ExpectedConditions.elementToBeClickable(By.partialLinkText("Taschen"))
);
aLink.click();
You are not using java? Update your question to show your code and add a language tag to your question. Your brain isn't a network connected device and we only know what you tell us. :-)
You can also use the XPath as below:-
//a[#title='Taschen']/text()
Code will be as below:-
WebElement tash = driver.findElement(By.xpath("Taschen"));
If you need to get the text then you can use :-
String tash = driver.findElement(By.xpath("Taschen")).getText();
You can also use the contains function in XPath
//a[contains(.,'Taschen')]
Hope it will help you :)

Not able to identify a lenghty linktext in selenium webdriver

I want to identify an element by linktext, I am facing strange issue
If the linktext value is short i.e addFirst, addLast will be able to locate the element by using
driver.findelement(By.linktext("addLast, addFirst")).click
IF the linktext is addmanualreferraltocheckthelenghtF, addmanualreferraltocheckthelenghtF lengthy as above not able to identify the element
Please help me to find the solution
Why don't you try partial link text.
driver.findelement(By.partialLinkText("addLast, addFirst")).click
try out contains method. use the XPATH value and in XPATH use contains for link text.
EG:
.//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]
driver.findelement(By.xpath("//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]").click
Alternatively you can also use starts with XPATH value.
.//*[starts-with(#linktext,'addmanual')]
driver.findelement(By.xpath("
//*[starts-with(#linktext,'addmanual')]").click
Hope it helps you.

selenium webdriver Findelement confused

I have looked around forum and haven't find right answer for my misunderstanding.
Let's say we have html code:
<h3 id="param_search_0" class="mygray open" data-searchparam="any42" data-id="0">Text here</h3>
So if I get element by tag "h3" like this:
IWebElement get_element = Element_from_above.FindElement(By.Tag("h3"));
What element will I get (select for later use), just text? If yes so how can I select full line element, or seperate, text and inside parameters, from <h3></h3> loop.
Thank you and sorry for this maybe stupid question.
Giving you an answer in Java, as you have not specified the language that you're using:
Suppose that you've created a web-driver instance:
WebDriver driver = new FirefoxDriver();
First, you need to access the web-page in which you are interested, using the URL of that web-page:
driver.get(url);
Then, you can either get the first <h3> element in the web-page:
WebElement element = driver.findElement(By.tagName("h3"));
Or you can get a list of all the <h3> elements in the web-page:
List<WebElement> elements = driver.findElements(By.tagName("h3"));
Then, you can get various attributes of that element.
For example, in order to get "the entire line" as you call it:
String line = element.getAttribute("outerHTML");
For example, in order to get "just the text" as you call it:
String line = element.getAttribute("innerHTML");
And in a similar manner, you can get the value of each and every attribute within the element...

Selenium: How to enter white space as the value of a text field?

I have a form text field that is being populated with a default value. I would like to clear the value and enter white space to assert that the expected validation occurs. I am using Selenium RC and the PHPUnit Selenium extension. How can I achieve this?
Note: I have already tried doing these, but they do not work:
$this->type('FirstName', " "); //spaces
$this->type('FirstName', "\t"); //tab character
They clear the value from the field, but they do not enter the white space into the field.
Update: I found that if you are using Selenium IDE, you can type white space by using their ${space} variable but this does not work when using Selenium RC and the PHPUnit Selenium extension.
recently I am having the same problem just like Andrew where the ${space} isn't work in Selenium RC. I found out that Zugwalt's solution is useful. Here is what I did to capture the whitespace.
selenium.focus("txt_textbox1");
selenium.keyPressNative("32");
strOutput = selenium.getEval("this.browserbot.findElement('txt_textbox1').value = ' ';");
Hope this help ;)
THanks #!
I fixed this in the phpunit-selenium code on github and made a pull request to the maintainer.
Here is my gist if anyone cares:
https://github.com/undernewmanagement/phpunit-selenium/commit/1b783ba8cfe4736f525b316a75a991e0a701afd1
You could use javascript injection and manipulate the value directly
selenium.getEval("this.browserbot.findElement('FirstName').value = ' ';");
Note this will not fire off any events so you would have to manually trigger them if desired.
How about setting it's value to an empty string ('')?
How about using the keyPress() function to "press" the spacebar?
You can try selenium.typeKeys('FirstName'," ");
Please let me know if it worked.
This should work for phpunit with Selenium:
class MyTest extends PHPUnit_Extensions_Selenium2TestCase
{
public function testSomething()
{
$textField = $this->byCssSelector('.blah');
// ...
$textField->clear();
$textField->value(" ");
// test assertion to follow
}
}
If you want to enter a sentence, the following won't work:
$textField->value("this is a sentence.");
The following will:
$textField->value("this");
$textField->value(" ");
$textField->value("is");
$textField->value(" ");
$textField->value("a");
$textField->value(" ");
$textField->value("sentence.");