简体   繁体   English

Javascript 按钮单击在 selenium webdriver 中不起作用

[英]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 单击元素的方法:

  1. Browse in the target page where the button is located(Tried with FireFox).在按钮所在的目标页面中浏览(用 FireFox 试过)。
  2. Right click on the button and select Inspect elements.右键单击按钮并选择检查元素。
  3. Right click on the HTML code of the button and in Copy select XPath.右键单击按钮的 HTML 代码,然后在复制中选择 XPath。
  4. Now you have the XPath copied to your clipboard.现在您已将 XPath 复制到剪贴板。

Now you just have to call this line:现在你只需要调用这一行:

document.getElementByXPath("PASTE THE XPATH HERE").click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM