sendkeys doesn't work on input field - java

I'm having a strange problem. sendkeys doesn't work on an input field although I am able to do a click on it.
I also tried setAttribute and even that doesn't work.
The input field is constantly monitoring user input and triggers a javascript when the user enters x number of characters. Is this causing the problem?
I tried these:
driver.findElement(By.xpath("//input[#id='id']")).sendKeys(c‌odeStr);
((JavascriptExecutor) driver).executeScript("document.getElementById('id').value='‌
123456'");
((JavascriptExecutor)
driver).executeScript("document.getElementById('id').
setAttr‌ibute('value', '123456')");
This is the error I receive:
org.openqa.selenium.NoSuchElementException: Unable to locate element: //input[#id='id']
but the click works fine:
driver.findElement(By.xpath("//input[#id='id']")).click();

Related

how to get a div span class element

i am trying to display the number 1 in my console, apparently it did not work, any advise please
This is my code apparently it didn't work.
String error1 = driver.findElement(By.xpath("//div[#class='label label-error']/span[#class='sidebar-label']")).getText();
the console error output
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='label label-error']/span[#class='sidebar-label']"}
Your Xpath is Wrong, Use one of these:
//div[#class="sidebar-label"]/span
//div[#class="sidebar-label"]/span[#class="label label-error"]
//*[#class="label label-error"]

Java Selenium: Get get multiple WebElements of the same class

This is the Button I want to press:
<button class="ProfileTweet-actionButton js-actionButton js-actionRetweet" data-modal="ProfileTweet-retweet" type="button">
The same kind of Button exists multiple time on the site.
I tried pressing it by doing this:
By byaXpath = By.xpath("//div[contains(#class,'ProfileTweet-actionButton js-actionButton js-actionRetweet')]");
WebElement Element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byaXpath));
Element.click();
But after 10 secons I just get this error:
org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for presence of element
Im kinda lost now. I tried diffrent solution to similar cases but nothing seems to work.
You have to put button instead of div at the start of your xpath.
"//button[contains(#class,'ProfileTweet-actionButton js-actionButton js-actionRetweet')]"

I have tried all possible ways to locate an element using Webdriver, but getting an error "Unable to locate Element"

I am new to Automation, and trying to automate a website. I was able to login to the site using webdriver, however I am unable to locate any elements on the next page.
I have tried locating it by id, name, css selector, linktext, xpath etc. I even put an explicit wait but that was of no help either.
I get the following error every time I try to locate the element:
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Owner to Channel "}
Here is the page source detail for the above mentioned button:
Page source for the button
Code to locate the button:
WebElement el = driver.findElement(By.linkText("Owner to Channel "));
el.click();
Any suggestions? May be I am missing something.
Thank you all for your suggestions.
I found that there was a frame in the page source, but it didn't have an id or name.
By hit and trial, I tried switching to the frame with id 0, and it worked!

Why can i not enter text into a field in Selenium/HtmlUnit?

Ok so i have been working on a program that can acces twitter via html unit/selenium drivers in java. This is part of a larger program i am trying to build. Regardless, I have succeced in getting the program to login to twitter with my account, and even getting it to search for other accounts but when i try to tweet, i get an error message like this:
"Exception in thread "main" java.lang.UnsupportedOperationException: You may only set the value of elements that are input elements
at org.openqa.selenium.htmlunit.HtmlUnitKeyboard.sendKeys(HtmlUnitKeyboard.java:82)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.sendKeys(HtmlUnitWebElement.java:343)
at javaapplication4.JavaApplication4.main(JavaApplication4.java:41)
Java Result: 1"
Im not sure what the issue is. Here is my code:
WebDriver driver = new HtmlUnitDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://twitter.com/login");
System.out.println(driver.getCurrentUrl());
WebElement element = driver.findElement(By.name("session[username_or_email]"));
element.sendKeys("A*****K*****");
WebElement pass = driver.findElement(By.name("session[password]"));
pass.sendKeys("*******");
pass.sendKeys(Keys.ENTER);
System.out.println(driver.getCurrentUrl());
WebElement tweet = driver.findElement(By.id("tweet-box-mini-home-profile"));
tweet.click();
WebElement tweet2 = driver.findElement(By.id("tweet-box-global-label"));
tweet2.sendKeys("A");
WebElement send = driver.findElement(By.className("btn primary-btn tweet-action tweet-btn js-tweet-btn"));
send.click();
driver.close();
There seem to be two different elements with name = session[username_or_email]. By default, Selenium receives the first one it finds from markup. That one is hidden, so it would make more sense if you received an error "Element is not visible and hence cannot be interracted with". Anyway, try to be more spefic when defining your locator, ie:
WebElement element = driver.findElement(By.xpath("//form[#class=\"t1-form clearfix signin js-signin\"]//input[#name=\"session[username_or_email]\"]"));
EDIT:
Sorry, I did not read the question all that well. Anyway, why are you sending your text to:
WebElement tweet2 = driver.findElement(By.id("tweet-box-global-label"));
that seems to correspond to:
<span id="tweet-box-global-label" class="visuallyhidden">Tweet text</span>
From what I see from Twitter, you should send your text to the first element:
WebElement tweet = driver.findElement(By.id("tweet-box-mini-home-profile"));
Anyway, seems like Javascript is doing something there onClick, so you probably need to look up this element again before sending text to it, otherwise you might see a StaleElementException there.

Finding and switching to the right frame with Selenium Webdriver

I am trying to write a WebDriver test for the following site for learning purposes: http://www.mate1.com
On clicking the login link, I get a form to fill in my email id and password. As far as I understand, the form is shown in an iframe.
To enter the credentials, I tried to identify the number of iframes for that particular page (and found it as 7) and tried switching in to each iframe and search for the email field by XPath and the ID. However, I wasn't successful to find it. So how can this be done?
This is my code:
driver.get("http:www.mate1.com");
driver.findElement(By.xpath("//*[#id='header-container']/div/a")).click();
List <WebElement> frames = driver.findElements(By.tagName("iframe"));
System.out.println("Totalframes -> "+ frames.size());
driver.switchTo().frame(0);
driver.findElement(By.xpath("//[#id='email']")).sendKeys("xxxxxxxxx#rocketmail.com");
This is likely a good situation to use switchTo().frame(WebElement):
driver.switchTo().frame(findElement(By.cssSelector(".iframe-container>iframe")));
The login and password fields are not in an iframe, you can directly use the following code -
driver.findelement(By.id("email").sendKeys("xxxxxxxxx#rocketmail.com");
Try not to switch to any iframe and execute the above line, this will work.

Categories