CCOthers
CCOthers

Reputation: 21

xpath found in chrome/firefox console, but not found by selenium?

xpath found in chrome/firefox console, but not found by selenium ? any suggestion ?

enter image description here

And try chropath(chrome plugin) none of them work: enter image description here

Java code both way:

 List<WebElement> Elements = driver.findElements(By.xpath("/html/body/div[2]/div/div[1]/div[2]/div[2]/div[1]/div/div[3]/div[1]/div[4]/div[2]/textarea"));
new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.xpath("/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/div[3]/div[1]/div[4]/div[2]/textarea[1]"))).sendKeys("testing");

Here is what the Selenium give:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: /html1/body1/div2/div1/div1/div2/div2/div1/div1/div3/div1/div[4]/div2/textarea1 (tried for 30 second(s) with 500 MILLISECONDS interval) at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:82) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:231) at message.hello.main(hello.java:148) Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: /html1/body1/div2/div1/div1/div2/div2/div1/div1/div3/div1/div[4]/div2/textarea1 For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z' System info: host: 'daviss-MacBook-Pro.local', ip: '2600:1010:b06c:96a7:d93d:6b6e:3c3d:3826%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.2', java.version: '10.0.1'

Would like to put some text into the text area. if this possible with selenium ?

Also confirm that the page doesn't have any thing to do with iframe

enter image description here

Upvotes: 0

Views: 2683

Answers (5)

beautifulMind
beautifulMind

Reputation: 21

Just refresh the page :)

I had the same issue. The absolute xpath was valid and could be found on developer console on Firefox as well as Chrome. However, WebDriver was only able to find the element on Chrome and was not able to find it on Firefox. I tried many things but nothing worked. Finally I decided to refresh the page using WebDriver and, voilla, after that the same code started working in Firefox and continued to work in Chrome. So just insert the below line above the code where you are trying to locate the element using the absolute xpath.

driver.navigate().refresh();

Upvotes: 2

A couple of reasons/options, you can try here-

  1. Try to clear and click first on that box and then use sendKeys()
  2. Check the visibility of that element in the webpage.
  3. Element width and height should not be zero.
  4. Make sure that box is enabled when you are trying to use sendKeys().
  5. Try with some wait time before finding this element.

Upvotes: 0

Sarthak Gupta
Sarthak Gupta

Reputation: 392

Try with javascript executor to send the value to the WebElement:

    WebElement wb = driver.findElement(By.xpath("/html/body/div[2]/div/div[1]/div[2]/div[2]/div[1]/div/div[3]/div[1]/div[4]/div[2]/textarea"));
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("arguments[0].value='(222)222-2222';", wb);

Upvotes: 0

Dinesh Deshmane
Dinesh Deshmane

Reputation: 26

Try using array list if you need to use List of element.

List<WebElement> Elements = driver.findElements(By.xpath("/html/body/div[2]/div/div[1]/div[2]/div[2]/div[1]/div/div[3]/div[1]/div[4]/div[2]/textarea"));

ArryaList <String> elementList = new ArryaList<>();
for (WebElement ele : Elements){
elementList.add(ele)
}

new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOf(elementList.get(0));
elementList.get(0).click();

Upvotes: 0

KunduK
KunduK

Reputation: 33384

Try this below XPath it should work.

driver.findElement(By.xpath("//textarea[@class='textarea']")).sendKeys("testing");

OR

driver.findElement(By.xpath("//textarea[@placeholder='inputthere~']")).sendKeys("testing");

In case if it gives error.Please check any iframe is avaiable? if avaialble then you have to switched to iframe first.

driver.switchTo().frame("Frame-Name") 

Hope this help you!

Upvotes: 1

Related Questions