简体   繁体   English

没有名字或ID的Selenium点击按钮

[英]Selenium clicking button with no name or id

I've been struggling with trying to automate this page. 我一直在努力尝试自动化这个页面。 After I login, I'm at this page where I'm supposed to click a button before I redirects me to the next page. 登录后,我在这个页面上,在我将我重定向到下一页之前,我应该点击一个按钮。 The problem is that this button does not have a name or an ID which is making it very difficult to find this element. 问题是这个按钮没有名称或ID,这使得很难找到这个元素。 I've tried mechanize and splinter both. 我试过机械化和分裂。 And finally tried selenium but that didn't help either. 最后尝试了硒,但这也没有帮助。 Really struggling with this. 真的很挣扎。 Just need to click this damn button! 只需要点击这个该死的按钮! Any help would be really really appreciated. 任何帮助都会非常感激。 Love python and automation, but this time nothing seems to be working for me. 喜欢python和自动化,但这次似乎没有什么对我有用。 Please find below a snapshot showing the the code shown when I click on "inspect element". 请在下面找到一张快照,显示我点击“检查元素”时显示的代码。 Also, I can't type here the page source code as it is >300000 characters, so you can probably take a look at the page (you'll need to login which takes just 10 seconds). 此外,我不能在这里键入页面​​源代码,因为它是> 300000个字符,所以你可以看看页面(你需要登录,只需10秒)。 The page I'm referring to is right after you login - http://www.160by2.com/Index 我所指的页面就在您登录后 - http://www.160by2.com/Index

[! [! Snapshot showing the code I get when I click "inspect element" [ 快照显示我点击“检查元素”时得到的代码[ ] [1] ] 1 ] 1

There is class name : 班级名称

driver.findElement(By.className("da-sms-btn"). driver.findElement(By.className( “DA-SMS-BTN”)。

Also you can open application in Chrome and copy CSS or XPATH in browser: 您还可以在Chrome中打开应用程序并在浏览器中复制CSS或XPATH:

  1. Open application in Chrome 在Chrome中打开应用程序
  2. Inspect element 检查元素
  3. Right click on highlighted area 右键单击突出显示的区域
  4. Copy CSS or XPATH 复制CSS或XPATH

您可以尝试首先通过id获取表单,然后按类名获取按钮:

wd.find_element_by_id("frmDashboard").find_element_by_class_name("da-sms-btn").click()

You can try to find the element through its xpath. 您可以尝试通过其xpath查找元素。 Selenium does a good job of this: Selenium做得很好:

webdriver.find_element_by_xpath("element xpath").click()

You can easily find the xpath by going to the inspect element sidebar on Chrome and right clicking on the element you want. 您可以通过转到Chrome上的inspect元素侧栏并右键单击所需的元素来轻松找到xpath。 The right click drop down menu should have an option "copy xpath". 右键单击下拉菜单应该有一个选项“copy xpath”。

I would write a cssSelector as follows and try that. 我会写一个cssSelector如下,并尝试。

button[onclick*='aSMS']

Notice, I am doing a partial search with * 请注意,我正在进行部分搜索*

Thank you so much for your replies! 非常感谢您的回复! I was actually able to make it work using splinter instead of selenium! 我实际上能够使用碎片而不是硒来使其工作! Here's what I did- 这是我做的 -

1) I just executed the js which was being executed on the onclick event of the button. 1)我刚刚执行了在按钮的onclick事件上执行的js。

jsstring="window.parent.openPage('SendSMS?id="+id+"', 'aSendSMS', 'aSMS', 'ulSMS')"
br.execute_script(jsstring)

2) Subsequently, I was faced with the problem that on the next page, everything was embedded inside iframe, and so find_by_name and all were not able to find elements. 2)随后,我面临的问题是,在下一页上,所有内容都嵌入在iframe中,所以find_by_name和所有都无法找到元素。 For that, splinter provides a nice method (Which is documented REALLY bad, so had to figure it out myself with help from stackoverflow) 为此,splinter提供了一个很好的方法(记录得很糟糕,所以不得不在stackoverflow的帮助下弄明白)

with br.get_iframe('iframe_Name') as iframe:
     iframe.fill("Element_to_fill_Name","Text_to_fill")
     iframe.find_by_tag("TagName")[Index_number].fill("Text_To_Fill")

Worked out brilliantly! 出色地完成了! Thanks everyone :) 感谢大家 :)

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

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