通过带有JavaScript的Python通过Selenium使WebElement可见 [英] Make WebElement visible via Selenium with Python with JavaScript

查看:266
本文介绍了通过带有JavaScript的Python通过Selenium使WebElement可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过selenium上传png。我的问题是,我需要使用的输入,是不可见的硒,但不是用户。在Selenium的常见问题中,他们告诉我使用JavascriptExcecutor像:

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

在过去,我用这个与C#,并且它的工作,但现在im奋斗转换使用到python。我将使用 document.getElementByName()函数,但输入没有名称,并且在页面上有多个。什么是解决这个问题的最好方法。我已经尝试过

  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



但是没有效果,我得到一个语法错误

解决方案

有一个 execute_script() 方法的驱动程序实例,参数传递给它类似于C#的 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)


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

解决方案

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)

这篇关于通过带有JavaScript的Python通过Selenium使WebElement可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆