53762

Selenium Twitter java

Question:

I'm tryng to get connected to my twitter account using Selenium Webdriver

WebDriver driver = new FirefoxDriver(); driver.get("https://www.twitter.com/login/"); WebElement formElement = driver.findElement(By.cssSelector("form.signin")); List<WebElement> allFormChildElements = formElement.findElements(By.cssSelector("input")); for(WebElement item : allFormChildElements ) { System.out.println("<"+item.getTagName()+"> "+ item.getAttribute("name") ); switch(item.getAttribute("name")) { case "session[username_or_email]": item.sendKeys(username); break; case "session[password]": item.sendKeys(password); break; } }

But i'm getting this error log :

<input> session[username_or_email] Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 38 millisecon

ds

I don't understand because it is printing the name of the input, why is not visible ? Any ideas ?

Thanks

Answer1:

Found that By inspecting the DOM of the twitter login page has two forms having almost same properties and three username and password fields are found.That's why you're getting ElementNotVisibleException[See Image]

<a href="https://i.stack.imgur.com/aQvh7.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/aQvh7.png" data-original="https://i.stack.imgur.com/aQvh7.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

So for that you have to go with relative xpath or css selectors or implement a logic for that .I'm proving two ways you can handle that situation

And I don't know why you are iterating over all the input fields and then finding the element by checking its attributes.You can simply call driver.findElements(By.name())

<strong>Relative Xpath</strong>

As I found that the input inside the @class signin-wrapper is the visible one,I've gone for this xpath

driver.get("https://twitter.com/login/"); driver.findElement(By.xpath("//div[@class='signin-wrapper']//input[@name='session[username_or_email]']")).sendKeys("viaxpath"); driver.findElement(By.xpath("//div[@class='signin-wrapper']//input[@name='session[password]']")).sendKeys("viaxpath");

<strong>Get the Visible Element</strong>

As the name implies it will find all the elements and return the element that is only visible

driver.get("https://twitter.com/login/"); getVisibleElement(driver, "session[username_or_email]").sendKeys("viavisibleName"); getVisibleElement(driver, "session[password]").sendKeys("viavisibleName"); public static WebElement getVisibleElement(WebDriver driver, String name) { List<WebElement> elements = driver.findElements(By.name(name)); for (WebElement element : elements) { if (element.isDisplayed()) { return element; } } return null; }

Recommend

  • Access to Attributes using a String
  • How to get the node and keyboard reference of TVML textField in TVJS?
  • Import XML file into PostgreSQL Database
  • Parsing an XML with Xpath in PHP
  • How to get selected option value in protractor
  • What's wrong about request.getSession().setAttribute()
  • How to copy a product's attributes value to another attribute in Magento?
  • Modifying Existing XML Content in C#
  • How to replace whole HTMLElement's text to something else?
  • I can't grab specific URL in search page
  • Java : For String values we use the command sendKeys(“String”); Similarly what is the command for po
  • 'this.function' is not a function - OO JS
  • Retrieving the value from a Selenium Webdriver WebElement field and passing it to a java variable
  • Ionic2, ion-range custom content of the range pin
  • Activate Word window from Excel with VBA
  • How to use custom customer variables in transactional email templates in magento?
  • How to select all local links in jQuery
  • Password-protect the macros of a second workbook from a first using VBA
  • addEventHandler() in a loop has unexpected results
  • Sending keystrokes from a C# application to a Java application - strange behaviour?
  • Google Contacts API get phone number (PHP)
  • How to populate html table with info from list in django
  • Prevent page break in text block with iText, XMLWorker
  • Shouldn't else be indented in the below code
  • Authentication in Play! and RestEasy
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • MongoError: Incorrect arguments
  • Django rest serializer Breaks when data exists
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Change an a tag attribute in JavaScript based on screen width
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Getting error when using KSoap library to consume .NET web services
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • UserPrincipal.Current returns apppool on IIS