通过带有JavaScript的Python通过Selenium使WebElement可见 [英] Make WebElement visible via Selenium with Python with JavaScript
问题描述
我想通过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 triedicon = 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#'sJavascriptExecutor
: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屋!