seleniumsvgxpathcss-selectorssvg-rect

How to click on a rect tag in a bar graph using Selenium


I have bar graph in a website and need to click on it. Using developer tools, I am getting below xpath:

/html/body/root/main/portal/div[2]/div/div/div/my-portal/div[3]/td-tyr/td-tyr/div[1]/filter-bar/div/div[3]/div[1]/div[1]/bar-chart/div/div[1]/div/svg/g[4]/g[1]/rect[1]

When using the above in xpath, it's giving invalid expression. Any help will be appreciated.


Solution

  • To click on the Bar Graph as the element have a parent <svg> element you can use either of the following Locator Strategies:


    Ideally to click on a clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:


    References

    You can find a couple of relevant discussions on interacting with SVG element in: