Asked  7 Months ago    Answers:  5   Viewed   196 times

I have an SVG object with a few rectangle elements. Using geckodriver, I am trying to click on one of the main SVG object. However with xpath-checker I am unable to detect the proper xpath for the same.

Till now, I am able to drilldown through xpath upto:

id('avg_score_chart')/div/div[1]/*[local-name()='svg' and namespace-uri()='http://www.w3.org/2000/svg']

My HTML code is as follows:

<div id="avg_score_chart" class="chart" style="height: 250px; color: black ! important; overflow: hidden; text-align: left;">
<div class="amcharts-main-div" style="position: relative;">
<div class="amcharts-chart-div" style="overflow: hidden; position: relative; text-align: left; width: 525px; height: 212px; padding: 0px;">
<svg version="1.1" style="position: absolute; width: 525px; height: 212px; top: 0.450012px; left: -0.5px;">
<desc>JavaScript chart by amCharts 3.17.1</desc>
<g>
<g>
<g>
<g>
<g>
<g>
<g transform="translate(60,52)">
<g transform="translate(96,41)">
<g transform="translate(96,123)">
<g transform="translate(96,123)">
<path cs="100,100" d="M0.5,0.5 L0.5,-81.5 L30.5,-81.5 L30.5,0.5 L0.5,0.5 Z" fill="rgb(242,244,28)" stroke="rgb(242,244,28)" fill-opacity="0.8" stroke-width="1" stroke-opacity="0.8">
</g>
<g transform="translate(318,123)">
<g transform="translate(318,123)">
<g transform="translate(318,123)">
</g>
</g>
<g>
<g>
<g>
<g>
<g>
<g>
<g>
<g>
<g>
<g>
<g>
</svg>

Can anyone help me out please?

 Answers

16

Try following XPath and let me know if problem still persist:

//div[@id="avg_score_chart"]//*[name()="svg"]

For <g> elements:

//div[@id="avg_score_chart"]//*[name()="svg"]/*[name()="g"]

Update

Finally, this should be nearly the best option:

//div[@class="portlet light boxshadow"][contains(.,"Store Wise Performance")]/div//div[@class="amcharts-chart-div"]/*[name()="svg"]//*[name()="g"]/*[name()="path" and @fill="rgb(242,244,28)"]
Tuesday, June 1, 2021
 
Rudie
answered 7 Months ago
73

To invoke click() on the reCaptcha checkbox as the element is within an <iframe> you need to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
  • Induce WebDriverWait for the desired elementToBeClickable.
  • You can use the following solution:

    • Code Block:

      public class ReCaptcha_click {
      
          public static void main(String[] args) {
      
              System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
              ChromeOptions options = new ChromeOptions();
              options.addArguments("start-maximized");
              options.addArguments("disable-infobars");
              options.addArguments("--disable-extensions");
              WebDriver driver = new ChromeDriver(options);
              driver.get("https://rsps100.com/vote/760");
              new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]")));
              new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();
          }
      }
      
  • Browser Snapshot:

    reCaptcha

Tuesday, June 15, 2021
 
Bharanikumar
answered 6 Months ago
85

This error message...

selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver

...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.


You need to take care of a couple of things:

  • Ensure that you have downloaded the exact format of the ChromeDriver binary from the download location pertaining to your underlying OS among:

    • chromedriver_linux64.zip: For Linux OS
    • chromedriver_mac64.zip: For Mac OSX
    • chromedriver_win32.zip: For Windows OS
  • Ensure that /etc/hosts file contains the following entry:

    127.0.0.1 localhost 
    
  • Ensure that ChromeDriver binary have executable permission for the non-root user.

  • Ensure that you have passed the proper absolute path of ChromeDriver binary through the argument executable_path as follows:

    with webdriver.Chrome(executable_path=r'C:pathtochromedriver.exe', chrome_options=chrome_options) as driver:
    
  • So your effective code block will be:

    options = Options()   
    options.add_argument("--headless")
    options.add_argument('--no-sandbox') # Bypass OS security model
    options.add_argument('--disable-gpu')  # applicable to windows os only
    options.add_argument("--disable-dev-shm-usage")  # overcome limited resource problems
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    with webdriver.Chrome(executable_path=r'C:pathtochromedriver.exe', options=options) as driver:
        driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before 
        driver.execute_script("document.body.style.zoom='150%'")
        driver.get("file:\"+url) # takes one argument, which is the url of the website you want to open
        driver.find_element_by_tag_name('body').screenshot(output)  # avoids scrollbar
    

Mandatory Considerations

Finally, to avoid incompatibility between the version of the binaries you are using ensure that:

  • Selenium is upgraded to current levels Version 3.141.59.
  • ChromeDriver is updated to current ChromeDriver v79.0.3945.36 level.
  • Chrome is updated to current Chrome Version 79.0 level. (as per ChromeDriver v79.0 release notes)
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • Execute your @Test as non-root user.

References

You can find a couple of reference discussions in:

  • Python Selenium “Can not connect to the Service %s” % self.path in linux server
  • selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe while opening chrome browser
  • How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
Friday, August 6, 2021
 
Haradzieniec
answered 4 Months ago
34
public class LoginPageTest extends IntegrationTest {

private HtmlUnitDriver driver;

@Before
public void setup() throws MalformedURLException, UnknownHostException{
    driver = new HtmlUnitDriver(true);
    driver.get(System.getProperty("login.url"));
}

@Test
public void testAuthenticationFailureWhenProvidingBadCredentials(){
    driver.findElement(By.id("username")).sendKeys("fakeuser");
    driver.findElement(By.id("password")).sendKeys("fakepassword");     
    driver.findElement(By.id("login")).click();

    assertTrue(driver.getCurrentUrl().endsWith("failed"));
}

@Test
public void testAuthenticationSuccessWhenProvidingCorrectCredentials(){
    driver.findElement(By.id("username")).sendKeys("validuser");
    driver.findElement(By.id("password")).sendKeys("validpassword");
    driver.findElement(By.id("login")).click();

    assertTrue(driver.getCurrentUrl().endsWith("/<name_of_webapp>/"));
}

}

That's how I do it for example.

EDIT: I just noticed comments. Anyway my code shows how you test the actual login page with Selenium.

Saturday, August 7, 2021
 
muncherelli
answered 4 Months ago
10

First of all, you cannot use such a convoluted xpath, that kills the readability of your page objects. Your selectors should be as much concise as possible. Secondly, you should not use XPATH and use CSS selector. Now coming back to your question,

 @FindBy(css="span[class*='ui-icon-triangle-1-n']")
 WebElement b;

or try

 @FindBy(css="span[class*='ui-button-text']>span[class*='ui-icon-triangle-1-n']")
 WebElement b;
Thursday, November 18, 2021
 
Xavio
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share