python logo


Tag: selenium

selenium python

python selenium get html

selenium get link

selenium click button

Selenium can automatically click on buttons that appear on a webpage. In this example we will open a site and click on a radio button and submit button.

Related course
Browser Automation with Python Selenium

Selenium button click
Start by importing the selenium module and creating a web driver object. We then use the method:


drivers.find_elements_by_xpath(path)

to find the html element. To get the path, we can use chrome development tools (press F12). We take the pointer in devtools and select the html button we are interested in. The path will then be shown, as the example screenshot:

find_element_by_xpath Find element by xpath, using chrome dev tools
After we have the html object, we use the click() method to make the final click.
Full code:


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

Download Selenium Examples

selenium screenshot python

selenium download image

Selenium,a web automation framework, can be used to get the all of the image links from a webpage.In this article we’ll given an example of that.

Related course
Browser Automation with Python Selenium

Get image links
At first we import the selenium module and start the web driver object.


driver = webdriver.Chrome(chrome_options=options)

The webpage consists of html code, defined by tags. To show an image a webpage has a code:


<img src=".../image.jpg">

Then we find all the elements using the img tag (from html):


images = driver.find_elements_by_tag_name('img')

Finally we print the link of each image using:


for image in images:
print(image.get_attribute('src'))

Full code:


from selenium import webdriver

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('https://imgur.com/')

images = driver.find_elements_by_tag_name('img')
for image in images:
print(image.get_attribute('src'))

driver.close()

That will return all the image urls on the webpage.
To download import urllib and use the line:


urllib.urlretrieve(src, "filename.png")

You could also use wget, by using os.system();

Download Selenium Examples