Python selenium click image button

the html is below:

  • Python selenium click image button
  • python is

    driver.find_element_by_xpath("//img[contains(@src='resources/images/logout.png')]/parent::a").click()
    

    it giving error as below

       driver.find_element_by_xpath("//img[contains(@src='resources/images/logout.png')]/parent::a").click()
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 221, in find_element_by_xpath
        return self.find_element(by=By.XPATH, value=xpath)
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 681, in find_element
        {'using': by, 'value': value})['value']
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 164, in execute
        self.error_handler.check_response(response)
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 164, in check_response
        raise exception_class(message, screen, stacktrace)
    NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"//img[contains(@src=\'resources/images/logout.png\')]/parent::a"}' ; Stacktrace: 
        at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpu3t8lf/extensions//components/driver_component.js:8860)
        at fxdriver.Timer.prototype.setTimeout/<.notify (file:///tmp/tmpu3t8lf/extensions//components/driver_component.js:396) 
    


    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.

    Python selenium click image button

    Example

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import java.util.concurrent.TimeUnit;
    public class ImageClk{
       public static void main(String[] args) {
          System.setProperty("webdriver.gecko.driver",
    "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
          WebDriver driver = new FirefoxDriver();
          //implicit wait
          driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
          //URL launch
          driver.get("https://www.tutorialspoint.com/index.htm");
          // identify image
          WebElement l = driver.findElement(By.xpath("//img[@alt='Tutorialspoint']"));
          String s = l.getAttribute("title");
          System.out.println("Title attribute value is :" + s);
          l.click();
          driver.quit();
       }
    }

    Output

    Python selenium click image button

    Python selenium click image button

    Updated on 03-Apr-2021 10:52:22

    • Related Questions & Answers
    • How to click on a link using Selenium webdriver in Python.
    • How to click on hidden element in Selenium WebDriver?
    • How to click Allow on Show Notifications popup using Selenium Webdriver?
    • Selenium Webdriver submit() vs click().
    • How to use Selenium webdriver to click google search?
    • How to click on a link in Selenium with python?
    • How to click on across browsers using Selenium Webdriver?
    • How do you click on an element which is hidden using Selenium WebDriver?
    • How to click on a link in Selenium?
    • How to click button Selenium Python?
    • How to perform double click on an element in Selenium with python?
    • How to perform right click on an element in Selenium with python?
    • WebDriver click() vs JavaScript click().
    • How to install Selenium WebDriver on Mac OS?
    • How to perform double click on an element in Selenium?

    First post here because I couldn't find the anycodings_button answer to what I was looking for. I am anycodings_button trying to automate clicking the red button.

    enter image description here

    I automated the login to this site(can't say anycodings_button which one) and I added the wait function, anycodings_button but still, no matter whether I copy the full anycodings_button xpath, selector or other options that the anycodings_button copy function gives me inside "inspect anycodings_button element", I can't get Python to execute anycodings_button click the button.(might be because the anycodings_button button image serves as overlay - might be anycodings_button wrong but it looks to me like that).

    This is the code I've used:

    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome("C:\\Users\\XXXXXXXXXXX\\Desktop\\chromedriver_win32\\chromedriver.exe")
    driver.get("XXXXXXXXXXX")
    
    driver.find_element_by_id("e_user").send_keys("XXXXXXXXXXX")
    driver.find_element_by_id("e_pass").send_keys("XXXXXXXXXXX")
    driver.find_element_by_xpath("/html/body/div[4]/table/tbody/tr[5]/td[2]/input").click()
    time.sleep(5)
    

    driver.find_element_by_xpath("//button[@onclick="btn_click(this,'btn_2_1')"]").click()`

    The bold command is not working. The rest is anycodings_button fine.

    I tried find element by xpath, text anycodings_button name(početak prisustva na radu) and anycodings_button several other selenium find element by id anycodings_button options, but had no success.

    I guess the best way would be to find the anycodings_button onclick element and click on it, but I am a anycodings_button newbie in code programming so if someone anycodings_button could help me out I would be really anycodings_button grateful. Each button you see there has a anycodings_button unique onlick name. I'll add a couple more anycodings_button pictures because they might be helpful to anycodings_button find the solution.

    Thanks in advance.

    Sincerely, Djigzy

    enter image description here

    24

    Answers 1 : of How to automate Selenium to click image button - find element by onclick - Python

    you can try to use ActionChains element anycodings_python is your element which you found with anycodings_python xpath

    from selenium.webdriver import ActionChains
    actions = ActionChains(browser)
    actions.move_to_element(element).perform()
    actions.click().perform()
    

    You can read more about ActionChains anycodings_python here anycodings_python https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html

    0

    2022-10-01T20:04:01+00:00 2022-10-01T20:04:01+00:00Answer Link

    mRahman

    How to click image in Selenium Python?

    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.

    How do I click an image in Selenium?

    How to click on an image in selenium webdriver.
    driver . findElement( By . xpath (".//*[@id='gridview-1018']/table/ tbody /tr[3]/td[7]/div/a/img")). ... .
    WebElement temp = driver. findElement(By. ... .
    WebDriverWait wait = new WebDriverWait (driver, 60); wait. until( ExpectedConditions..

    Where is Xpath image in Selenium?

    Mastering XPath and CSS Selector for Selenium To fetch any attribute in Selenium, we have to use the getAttribute() method. The method takes the attribute name as a parameter. So to get the src attribute, we have to write getAttribute("src").