Python selenium click on visible text

I am trying to click to a button which has a visible text but I can't find a way.how it looks like . I am trying to click to the "laebel 41" but I need to click by detecting visible text

asked Aug 10, 2021 at 14:50

In order to click on the label element try this:

driver.find_element_by_xpath("//label[contains(text(),'41')]").click()

In order to click on input sibling element try this:

driver.find_element_by_xpath("//label[contains(text(),'41')]/..//input").click()

Don't forget to add some wait / delay before accessing the element to make it loaded.

answered Aug 10, 2021 at 15:31

Python selenium click on visible text

ProphetProphet

23.6k13 gold badges49 silver badges67 bronze badges

You can use this:

find_element_by_css_selector("input[type='radio'][value='41057']").click()

It is a radio button and if code doesn't work, you can search for radio button click

answered Aug 10, 2021 at 15:05

See if this works.

driver.find_element_by_xpath("//label[text()='41']//ancestor::div/input[@type='radio']").click()

OR
driver.find_element_by_xpath("//label[text()='41']/../input[@type='radio']").click()

answered Aug 10, 2021 at 15:11

itronic1990itronic1990

1,1032 gold badges3 silver badges17 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Selenium is a tool that provides APIs to automate a web application to aid in its testing. In this article, we discuss the use of Selenium Python API bindings to access the Selenium WebDrivers to click a button by text present in the button. In the following example, we take the help of Chrome. The method used is the find_element_by_link_text() which scrapes the element using the text present. In case there is no such element with the given text attribute, NoSuchElementException is returned.

    Installation:

    Make sure you have Selenium installed using

    pip3 install Selenium

    And also download the WebDriver for your web browser :

    Chrome : https://chromedriver.chromium.org/downloads
    Firefox : https://github.com/mozilla/geckodriver/releases
    Safari : https://webkit.org/blog/6900/webdriver-support-in-safari-10/

    Once Selenium is installed along with the desired WebDriver, we create a file script.py and using our code editor write the python script below which opens up the geeksforgeeks website using the Selenium WebDriver and clicks the Sign In button using the link text.

    Syntax:

    driver.find_element_by_link_text("sample text")

    Steps by step Approach:

    • Import required modules.
    • Create webdriver object.
    • Assign URL.
    • Use maximize_window() method to maximize the browser window. And then wait 10 seconds using sleep() method.
    • Use find_element_by_link_text() method to click button by text.

    Below is the implementation.

    Python3

    from selenium import webdriver

    import time

    driver = webdriver.Chrome(r"./driver/chromedriver")

    driver.maximize_window()

    time.sleep(10)

    button = driver.find_element_by_link_text("Sign In")

    button.click()

    Output:

    https://media.geeksforgeeks.org/wp-content/uploads/20210222231422/output_FTOFsx0Z_Tx7e.mp4

    First, the WebDriver opens up the window with geeksforgeeks, maximizes it, and waits for 10 seconds. Then it clicks the Sign In button and opens up the sign-up panel.

    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we can be running with Python.

    Different methods of Select class:

    • Selection an option from the dropdown menu by INDEX.
    • Selection an option from the dropdown by VISIBLE TEXT.
    • Selection an option from the dropdown menu by VALUE.

    we are discussing on visible text method in the drop-down list.

    The strategy chooses the alternative by its obvious choice label esteem. It acknowledges the noticeable content estimation of the choice tag and brings nothing back.

    Requirement: You need to install chromedriver and set path.Click here to download.for more information follows this link.

    Working with Drop-down list: Initially, you have to import the Select class and afterward you have to make the case of Select class. After making the case of Select class, you can perform select strategies on that occasion to choose the choices from the dropdown list.

    from selenium.webdriver.support.ui import Select

     for selection by using

    drop=Select(driver.find_element_by_id(‘ ‘)

    drop.select_by_visible_text(” “)

    Example: We will be doing the following:

    • Import selenium module
    • Import select class module
    • Using web page for drop down list (URL).
    • Navigate to the id of option bar.

    Python selenium click on visible text

    Python3

    Output:

    https://media.geeksforgeeks.org/wp-content/uploads/20200925012101/by-visible.mp4

    How do I get visible text in Selenium?

    To get the text of the visible on the page we can use the method findElement(By. tagname()) method to get hold of . Next can then use the getText() method to extract text from the body tag. WebElement l=driver.

    How do you click text in Selenium?

    We can select the text of a span on click with Selenium webdriver. To identify the element with span tag, we have to first identify it with any of the locators like xpath, css, class name or tagname. After identification of the element, we can perform the click operation on it with the help of the click method.

    How do you click text in Python?

    Steps by step Approach: Assign URL. Use maximize_window() method to maximize the browser window. And then wait 10 seconds using sleep() method. Use find_element_by_link_text() method to click button by text.

    How do you click on an image in Python Selenium?

    We can click on an image with Selenium webdriver in Python using the method click. First of all, we have to identify the image with the help of any of the locators like id, class, name, css, xpath, and so on. An image in the html code is represented by the img tagname. Let us see the html code of an image element.