Problem with writing in hidden textbox with Selenium

Asked

Viewed 305 times

0

I’m trying to write in a textbox and click on a button (these elements are initially hidden in the page) using Selenium (with Java), but I can’t find the element, I’ve tried searching for name, ID, tagname, classname, Xpath, I can’t manipulate in any way.

Follow screen print with inspect active elements. inserir a descrição da imagem aqui

What happens is that for this textbox and button to appear on the screen I need to click the request button (until that part I can): inserir a descrição da imagem aqui

Here the elements I mentioned are active: inserir a descrição da imagem aqui

In the code below I got the amount of elements that I find on the page that have the same id, name, tagname, classname or Xpath textbox that I want to manipulate (the first 2 lines is to press the request button, this part is working): inserir a descrição da imagem aqui

That is, in all forms of manipulating the textbox return that there is no such element (except classname, but of these 10 found elements none is the textbox that I want to manipulate, I have tested one by one).

As this page is part of a local portal of the company where I work, it is no use to pass a link to check, so I sent several prints.

Does anyone know any way to deal with these Hidden elements?

2 answers

0

You can use the class JavascriptExecutor to execute a Javascript code on the page itself, and leave the element visible:

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('_Justificativa').style.visibility = 'visible'");

You can set the value:

String texto = "Ola Mundo!";
jse.executeScript("document.getElementById('_Justificativa').value = '" + texto + "'");

And if necessary, you can click on the element as well:

jse.executeScript("document.getElementById('_Justificativa').click()");
  • Caio, this code would be to manipulate IE right? You could do this by manipulating Chrome? Anything I try to translate my code to manipulate IE and test this idea.

  • I tried using this code: Webelement txb =driver.findElement(By.name("_Justification")); jse.executeScript("Arguments[0].click();", txb); .

  • This code works for any browser, I believe! I even always use Firefox and never had problem, it is not to have headache with Chrome too. In relation to this error, it occurs because the element is hidden, and Selenium tries to find the Webelement anyway. What I imagine to be feasible and work, is what I posted in the answer, which would be a javascript "injection". Try to use it exactly like it is there, and give me an answer to see if it worked!

  • Now I did it. I realized that the textbox and the button were inside an iframe, and while searching the internet I saw that I had to use "driver.switchTo(). frame(0);" in order to be able to write to it. Javascript failed because it could not find any element with the name or id I passed.

  • I get it. great that you solved it! You can also switch to the frame by passing its ID as argument, if you need to, and for ease of viewing as well.

  • Thank you so much for your help. In my case it would not be right to go by ID or Name because these identifiers are not fixed in my iframe, each time I run the program they change, so I had to choose the index.

Show 1 more comment

0

Now I can fix it. For the next ones who have the same problem I do:

After going up in inspect element noticed that all that div that encompassed the text box and the button was an iframe, I saw on this site (https://www.guru99.com/handling-iframes-selenium.html) that needed to give the following commands:

driver.switchTo().frame(0);

        driver.findElement(By.name("_Justificativa")).sendKeys("teste");

In the case of the "0" of the "driver.switchTo(). frame(0)" is my iframe index, if you have more than one iframe on the page you will need to verify which one you need to handle, you can use the "driver.findElements(By.tagname("iframe").size());" command to identify the amount of iframes on the page.

For me it solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.