Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make WebElement visible via Selenium with Python with JavaScript

I'm trying to upload a png via selenium. My Problem is, that the Input I need to use, is invisible to selenium, but not to the user. In the FAQ of Selenium they told me to use the JavascriptExcecutor like:

((JavascriptExecutor)driver).executeScript("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", fileUploadElement);

I used this with C# in the past, and it worked, but now im struggeling to convert that usage to python. I would use the document.getElementByName() function, but the input doesn't have a Name and there are more than one on the page. What is the best way to solve that Problem. I already tried

icon = element.find_element_by_css_selector("input")
script_befehl = icon+".style.visibility = 'visible'; "+icon+".style.height = '1px'; "+icon+".style.width = '1px'; "+icon+".style.opacity = 1

but that also didn't work, i'm getting a Syntax error

like image 562
Chorgo Avatar asked Sep 08 '14 13:09

Chorgo


People also ask

How do I make WebElement visible in Selenium?

We can create a JavaScript Executor for making an element visible in Selenium webdriver. A hidden element has a style attribute whose value set to display: none. JavaScript Executor can make the same element visible on the page. Selenium executes JavaScript commands with the help of the executeScript method.

Can Selenium scrape JavaScript?

The Selenium browser driver is typically used to scrape data from dynamic websites that use JavaScript (although it can scrape data from static websites too). The use of JavaScript can vary from simple form events to single page apps that download all their content after loading.


1 Answers

There is an execute_script() method on the driver instance, arguments are passed to it in a similar to C#'s JavascriptExecutor:

icon = element.find_element_by_css_selector("input")
driver.execute_script("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", icon)
like image 162
alecxe Avatar answered Nov 12 '22 10:11

alecxe