最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Selenium clicking button with no name or id - Stack Overflow

programmeradmin3浏览0评论

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. 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. 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). The page I'm referring to is right after you login -

[!Snapshot showing the code I get when I click "inspect element" []1

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. 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. 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). The page I'm referring to is right after you login - http://www.160by2./Index

[!Snapshot showing the code I get when I click "inspect element" []1

Share Improve this question asked Aug 10, 2015 at 17:28 spandan madanspandan madan 3291 gold badge6 silver badges14 bronze badges 1
  • Is it in the only button element on the screen? – nivix zixer Commented Aug 10, 2015 at 17:34
Add a ment  | 

5 Answers 5

Reset to default 3

There is class name:

driver.findElement(By.className("da-sms-btn").

Also you can open application in Chrome and copy CSS or XPATH in browser:

  1. Open application in Chrome
  2. Inspect element
  3. Right click on highlighted area
  4. Copy CSS or XPATH

You can try first getting the form by the id, then getting the button by the class name:

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. Selenium does a good job of this:

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. The right click drop down menu should have an option "copy xpath".

I would write a cssSelector as follows and try that.

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.

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. For that, splinter provides a nice method (Which is documented REALLY bad, so had to figure it out myself with help from 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 :)

发布评论

评论列表(0)

  1. 暂无评论