Python Forum
How to trigger click event on Button without ID/Name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to trigger click event on Button without ID/Name
#1
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 SecurityPhrase---right-btn-container---32k8- ">
<button type="button" class="btn btn-success btn btn-default">YES</button>
</div>

When I inspect the element via Developer Tools, I got this highlighted, thus I right-click on them and select Copy via XPath.
Then I paste into the script it become like this,
/html/body/div[1]/div/div/div[2]/div[2]/div/div/div/div/div[2]/div[2]/div/div[2]
Then I add a .click() event, but the .click is not triggered.

Any idea?
Reply
#2
if you insist on using find_element_by_xpath you need to fix the xpath
or you can try using find_element_by_class_name

further reading https://selenium-python.readthedocs.io/l...ments.html
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I suppose you are using selenium.. It's possible that it's not working because you are calling click() on the div element that is wrapping the button, not sure.

Button tag from your example has classes though, so you can use that.
b = driver.find_element_by_class_name('btn-succes')

or you can use css selector:
b = driver.find_element_by_css_selector('.btn.btn-success.btn.btn-default')
Reply
#4
(Jan-27-2019, 12:02 PM)mlieqo Wrote: I suppose you are using selenium.. It's possible that it's not working because you are calling click() on the div element that is wrapping the button, not sure.

Button tag from your example has classes though, so you can use that.
b = driver.find_element_by_class_name('btn-succes')

or you can use css selector:
b = driver.find_element_by_css_selector('.btn.btn-success.btn.btn-default')

Yes. I am using Selenium, and I also did try using ClassName/Css Selector with .click() but still not working tho.
Reply
#5
Be aware that find_elements_by_css_selector and find_elements_by_xpath return a list so use [0].
button = browser.find_elements_by_css_selector('.btn')
button[0].click()
So CSS .btn and Xpath //button[@type='button'] are direct reference to button,of course if many buttons then need a more specific path.

For Path you have found.
time.sleep(3) # Add some time to give site time to load
button = browser.find_elements_by_xpath("/html/body/div[1]/div/div/div[2]/div[2]/div/div/div/div/div[2]/div[2]/div/div[2]")
button[0].click()
Reply
#6
(Jan-27-2019, 03:13 PM)snippsat Wrote: Be aware that find_elements_by_css_selector and find_elements_by_xpath return a list so use [0].
button = browser.find_elements_by_css_selector('.btn')
button[0].click()
So CSS .btn and Xpath //button[@type='button'] are direct reference to button,of course if many buttons then need a more specific path.

For Path you have found.
time.sleep(3) # Add some time to give site time to load
button = browser.find_elements_by_xpath("/html/body/div[1]/div/div/div[2]/div[2]/div/div/div/div/div[2]/div[2]/div/div[2]")
button[0].click()

Sorry for the late respond due to Chinese Lunar Year periods.
I did tried that as well. But it still won't trigger the button .click()

Please advise.
Reply
#7
It work for me if do a setup trick to test your code posted local.
Has put in JavaScript,to know that click on button work.
Most of course have url address to test this on site that you have copy code from.
import webbrowser, os

webbrowser.open(f'file:///{os.path.realpath("local.html")}')

# local.html
'''
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Title of the document</title>
  </head>
  <body>
    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 SecurityPhrase---right-btn-container---32k8- ">
      <button type="button" class="btn btn-success btn btn-default" onclick="alert('Hello world!')">Yes</button>
    </div>
  </body>  
</html>
'''
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time

#--| Setup
chrome_options = Options()
#chrome_options.add_argument("--headless")
#chrome_options.add_argument('--disable-gpu')
#chrome_options.add_argument('--log-level=3')
browser = webdriver.Chrome(executable_path=r'C:\cmder\bin\chromedriver.exe')
#--| Parse or automation
browser.get('file:///E:/div_code/scrape/local.html')
time.sleep(3)
button = browser.find_elements_by_xpath("/html/body/div/button")
button[0].click()
time.sleep(5)
browser.quit()
Reply
#8
(Feb-10-2019, 11:02 AM)snippsat Wrote: It work for me if do a setup trick to test your code posted local.
Has put in JavaScript,to know that click on button work.
Most of course have url address to test this on site that you have copy code from.
import webbrowser, os

webbrowser.open(f'file:///{os.path.realpath("local.html")}')

# local.html
'''
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Title of the document</title>
  </head>
  <body>
    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 SecurityPhrase---right-btn-container---32k8- ">
      <button type="button" class="btn btn-success btn btn-default" onclick="alert('Hello world!')">Yes</button>
    </div>
  </body>  
</html>
'''
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time

#--| Setup
chrome_options = Options()
#chrome_options.add_argument("--headless")
#chrome_options.add_argument('--disable-gpu')
#chrome_options.add_argument('--log-level=3')
browser = webdriver.Chrome(executable_path=r'C:\cmder\bin\chromedriver.exe')
#--| Parse or automation
browser.get('file:///E:/div_code/scrape/local.html')
time.sleep(3)
button = browser.find_elements_by_xpath("/html/body/div/button")
button[0].click()
time.sleep(5)
browser.quit()

Hi. This should do the trick, because I didn't include the time.sleep to let it wait for awhile to load. That's why failed.
But anyway, I am still don't know how to launch the chrome browser with some settings.
I have a make new post, but still don't get any response yet. please kindly advise from there.
chromedriver.exe
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  cant click button by script at page michael1834 1 1,053 Dec-08-2023, 04:44 PM
Last Post: SpongeB0B
  Click on a button on web page using Selenium Pavel_47 7 4,681 Jan-05-2023, 04:20 AM
Last Post: ellapurnellrt
  button click error rafarangel 2 3,121 Feb-11-2021, 08:19 PM
Last Post: buran
  Log In Button Won't Click - Python Selenium Webdriver samlee916 2 3,826 Jun-07-2020, 04:42 PM
Last Post: samlee916
  How to click facebook message button JanelleGuthrie 2 2,408 May-14-2020, 06:02 PM
Last Post: Larz60+
  Clicking on element not triggering event in Selenium Python (Event Key is not in data dkaeloredo 2 4,273 Feb-16-2020, 05:50 AM
Last Post: dkaeloredo
  Contact form button click action Man_from_India 1 2,791 Feb-01-2020, 06:21 PM
Last Post: snippsat
  HOWTO? Login DSL Modem with Python Requests: need Click "Apply" Button Webtest 4 8,479 Aug-20-2019, 04:03 PM
Last Post: johnmina
  Selenium click on popup button??? GuJu 7 7,868 Jul-20-2019, 09:21 AM
Last Post: Nizam
  how to click javascript button hcyeap 3 4,939 Aug-16-2018, 01:26 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020