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.

3
  • 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 Commented Feb 8, 2018 at 14:57
  • You can automate this with a python package that came from JAvascript 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. Commented Feb 8, 2018 at 15:00
  • You can also use Selenium, see stackoverflow.com/questions/8871654/… and pythonspot.com/selenium-click-button Commented Feb 8, 2018 at 15:02

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

-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/)

5 Comments

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?
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 selenium-python.readthedocs.io/locating-elements.html
In Firefox you can get the xpath in the web developer tools - right click element - copy - xpath
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:
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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.