[英]Javascript button click not working in selenium webdriver
I am trying to automate hitting a javscript button using a python script and web driver however no matter how I try to refer to the element it doesn't seem to activate the javscript.我正在尝试使用 python 脚本和网络驱动程序自动点击 javscript 按钮,但是无论我如何尝试引用该元素,它似乎都不会激活 javscript。 Here's an excerpt from the website I am trying to hit the button on:
这是我试图点击按钮的网站的摘录:
<li>
<a href="javascript:;" data-blogid="19079" id="picture-trigger">
<i class="glyphicon glyphicon-picture light-red"></i >
<span> Picture </span >
</a >
</li >
I've tried selecting by CSS selector, by XPATH and while I see the element being selected (doted line around it when running), nothing happens.我已经尝试通过 CSS 选择器、XPATH 进行选择,当我看到元素被选中时(运行时围绕它的虚线),没有任何反应。
I've also tried both.click() and.submit() neither one seems to work.我也试过 both.click() 和 .submit() 似乎都不起作用。 Here's my most recent attempt:
这是我最近的尝试:
element = mydriver.find_element_by_id("picture-trigger")
element.click()
I think perhaps the issue is the javascript:;我认为问题可能出在 javascript:; isn't being triggered when called from web driver the same way it is when an interactive user hits it but I'm at a loss for how else to automate clicking it.
从 Web 驱动程序调用时不会像交互式用户点击它时那样被触发,但我不知道如何自动点击它。
Does anyone with more web driver experience know why this isn't working or how I can get this working?有更多 web 驱动程序经验的人知道为什么这不起作用或我如何才能让它工作?
Thanks Brad谢谢布拉德
Here's what I see when I run the code:这是我运行代码时看到的内容:
Here's what it looks like when I manually click on the button:这是我手动单击按钮时的样子:
I think the issue lies in the fact that the site is using jquery.我认为问题在于该站点正在使用 jquery。 I was able to work around this using the following:
我能够使用以下方法解决此问题:
time.sleep(3);
mydriver.execute_script("$('.light-red').click();")
time.sleep(3);
mydriver.execute_script("$('.uploadbutton').click();")
There may be a better way than using time.sleep but I spent so much time fighting with trying to get the div to unhide that its good enough for now.可能有比使用 time.sleep 更好的方法,但我花了很多时间试图让 div 取消隐藏它现在已经足够好了。
Hopefully this helps someone else out in a similar situation.希望这可以帮助处于类似情况的其他人。 Thanks to everyone for all the help.
感谢大家的帮助。
Here is how I find my way to click on an element using XPath:以下是我如何找到使用 XPath 单击元素的方法:
Now you just have to call this line:现在你只需要调用这一行:
document.getElementByXPath("PASTE THE XPATH HERE").click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.