Python Forum

Full Version: How to trigger click event on Button without ID/Name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
<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?
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
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')
(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.
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()
(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.
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()
(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