简体   繁体   中英

how to click on an image using selenium web driver and python as scripting language

I am writing a selenium webdriver program using python.
My problem is that I am not able to click on the image by using x path or css path or id.

    driver.find_element_by_xpath("/div/svg/g/g[3]/g/rect").click()
    driver.find_element_by_css_selector(""div#servicecatalogdisplaygraph.x-component svg g g g rect")

My html page info is:

    <div id="servicecatalogdisplaygraph" class="x-component graph x-border-item x-box-item x-        component-default" style="background: none repeat scroll 0% 0% white; padding: 10px; overflow: auto; margin: 0px; width: 484px; left: 0px; top: 0px; height: 491px;">
    <svg style="width: 100%; height: 100%; min-width: 89px; min-height: 422px;">
    <g>
    <g>
    <g>
    <g>
    <g style="cursor: pointer;">
    <image xlink:href="/images/mxgraph/link.png" style="pointer-events:none" x="42" y="202" width="16" height="16" stroke-width="1" transform=" scale(1 1) translate(0 0)">
    <rect stroke="none" fill="none" visibility="hidden" pointer-events="fill" x="42" y="202" width="16" height="16" stroke-width="1">
    </g>
    </g>

First tip: use shorter selector when possible, you don't need this overhead. Lets use you code example (I'm assuming by your dom that the elements are descendants ), locating and clicking the image:

driver.find_element_by_css_selector("#servicecatalogdisplaygraph image").click()

or

driver.find_element_by_css_selector("#servicecatalogdisplaygraph rect").click()

Thats should work.

PS Be sure that the image element (or the rect one) is visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM