0

I want to automate clicking of a button which pops up on a webpage. How can I do it using python? I've zero experience in JavaScript and just started learning prgramming.

Below is the outer HTML of the button:

< button type="button" class="_23b4U" data-crid="16175cf391104e1db0234ea1707ff45c">Accept

I searched and found similar questions: How to auto click an input button

Automatic click on a pop up button

Please help.

  • You will not be able to use python for this. You would need to use Javascript as it will run in the browser and when the button pops up, you can target it and action a click event – Stretch0 Feb 08 '18 at 14:57
  • You can automate this with a python package that came from JAvascript https://pypi.python.org/pypi/pyppeteer These packages were originally written to automate testing of UI functionality, but, you can use them to scrape websites "loading" the page fully and using Cheerio or other libraries to easily find the button you want and "click" it. It's pretty slick. – Josh Lavely Feb 08 '18 at 15:00
  • You can also use Selenium, see https://stackoverflow.com/questions/8871654/how-to-press-click-the-button-using-selenium-if-the-button-does-not-have-the-id and https://pythonspot.com/selenium-click-button/ – Joe Feb 08 '18 at 15:02

2 Answers2

0

For automation you definitely might wanna check out

webbot

Its is based on selenium and offers lot more features with very little code like automatically finding elements to perform actions like click , type based on the your parameters.

Here is doc : https://webbot.readthedocs.io/

Natesh bhat
  • 12,274
  • 10
  • 84
  • 125
-1

Take a look at Selenium http://selenium-python.readthedocs.io/

from selenium import webdriver
import time

options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('http://codepad.org')

# click radio button
python_button = driver.find_elements_by_xpath("//input[@name='lang' and @value='Python']")[0]
python_button.click()

# type text
text_area = driver.find_element_by_id('textarea')
text_area.send_keys("print('Hello World')")

# click submit button
submit_button = driver.find_elements_by_xpath('//*[@id="editor"]/table/tbody/tr[3]/td/table/tbody/tr/td/div/table/tbody/tr/td[3]/input')[0]
submit_button.click()

(Code stolen from https://pythonspot.com/selenium-click-button/)

Joe
  • 6,758
  • 2
  • 26
  • 47
  • Hi, thanks for the responce. I tried the programme on the IDLE. The website opens but I'm not able to locate and correcly mention the x-paths. How can I get the correct x-paths? – Khushal Badhan Feb 09 '18 at 11:55
  • You need to look at the HTML file and get it from there. Instead of the x-path there are also other options to locate the element by id or by name. see http://selenium-python.readthedocs.io/locating-elements.html – Joe Feb 09 '18 at 12:04
  • In Firefox you can get the xpath in the web developer tools - right click element - copy - xpath – Joe Feb 09 '18 at 12:08
  • Yes I used different methods for locating different elements and almost done, but the last button is producing erroe when tried with by 'class method' This is what I tried for by class method: `give_access = driver.find_element_by_class_name("_1cPA3")` then for by css selector: #root > div > div > div._3Vu4w._3Xzhy > div._2Jdsd > div > div._3wkMv > div > button `give_access = driver.find_element_by_css_selector('#root > div > div > div._3Vu4w._3Xzhy > div._2Jdsd > div > div._3wkMv > div > button')` tried this Then for xpath: – Khushal Badhan Feb 09 '18 at 15:06
  • _Then for xpath:_ //*[@id="root"]/div/div/div[1]/div[2]/div/div[2]/div/button `give_access = driver.find_element_by_xpath("//*[@id='root']/div/div/div[1]/div[2]/div/div[2]/div/button")[0]` _But nothing is working._ _below is the element which I'm trying to locate:_ give_access = driver.find_element_by_xpath("//*[@id='root']/div/div/div[1]/div[2]/div/div[2]/div/button")[0] – Khushal Badhan Feb 09 '18 at 15:17