首页 > 解决方案 > Test execution halts with the xpath and unable to continue through Selenium

问题描述

I'm using xpath to click an "image" that has an "onclick" attribute. I have tried a few different xpaths but I can't get it to work.

HTML:

<img height="16" width="16" class="rollover" alt="Copy" src="images\copy.gif" onclick="TestExtract(93, true);">

py Code:

test_csv_copy_btn = driver.find_element_by_xpath("//img[@onclick='TestExtract(93, true);']").click()

标签: pythonseleniumxpathcss-selectorswebdriver

解决方案


As per your code trials, invoking click() on the desired element won't return anything. So assigning it to a variable will be of no use.

As per the HTML you have provided to click on the desired image you can use either of the following solutions:

  • css_selector:

    driver.find_element_by_css_selector("img.rollover[alt='Copy'][src*='copy'][onclick^='TestExtract']").click()
    
  • xpath:

    driver.find_element_by_xpath("//img[@class='rollover' and @alt='Copy'][contains(@src,'copy')][starts-with(@onclick,'TestExtract')]").click()
    

推荐阅读