Reputation: 31
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();
Upvotes: 2
Views: 1335
Reputation: 5072
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.
Upvotes: 1