Last modified: Nov 22, 2023 By Alexander Williams

Python Selenium: Click Button by Text (Examples)

Example 1: Click Button by Exact Text


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Click a button with exact text match
button_text = 'Click Me'
button = driver.find_element_by_xpath(f'//button[text()="{button_text}"]')
button.click()

# Close the browser
driver.quit()

Output:


# No specific output. The script clicks the button with the exact text match.

Example 2: Click Button by Partial Text Match


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Click a button with partial text match
partial_text = 'Click'
button = driver.find_element_by_xpath(f'//button[contains(text(), "{partial_text}")]')
button.click()

# Close the browser
driver.quit()

Output:


# No specific output. The script clicks the button with a partial text match.

Example 3: Click Nth Button by Text


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Click the nth button with specific text
button_index = 2  # Change to the desired index
button_text = 'Click Me'
button = driver.find_element_by_xpath(f'(//button[text()="{button_text}"])[{button_index}]')
button.click()

# Close the browser
driver.quit()

Output:


# No specific output. The script clicks the nth button with specific text.

Example 4: Click Button Inside Specific Element


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Find the parent element containing the button
parent_element = driver.find_element_by_id('parent-div')

# Click the button with specific text inside the parent element
button_text = 'Click Me'
button = parent_element.find_element_by_xpath(f'.//button[text()="{button_text}"]')
button.click()

# Close the browser
driver.quit()

Output:


# No specific output. The script clicks the button with specific text inside a parent element.

Example 5: Click Button Using Link Text


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Click a button using link text
button_text = 'Click Me'
button = driver.find_element_by_link_text(button_text)
button.click()

# Close the browser
driver.quit()

Output:


# No specific output. The script clicks the button using link text.

Example 6: Click Button Using Partial Link Text


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Click a button using partial link text
partial_text = 'Click'
button = driver.find_element_by_partial_link_text(partial_text)
button.click()

# Close the browser
driver.quit()

Output:


# No specific output. The script clicks the button using partial link text.