0

I have the following link, and I wish to click it using Selenium,

    <a data-toggle="tooltip" data-placement="middle" title="Nuova Richiesta" ui-sref="showRda({rdaID: 'new'})" style="color:#23282d; font-size: 16px;">
                                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Nuova Richiesta
                                </a>

I tried the following:

    driver.find_element_by_css_selector("a[ui-sref='showRda({rdaID: /'new/'})']").click()

But i got the following error

    InvalidSelectorException: invalid selector: An invalid or illegal selector was specified

Any advice?

Thanks, Andrea

3 Answers 3

1

Sometimes the driver....click() doesn't work, and you need to use the javascript execute. Example

python code:

element = driver.find_element_by_css_selector('...')
driver.execute_script("arguments[0].click();", element)
0

This is my selenium code with Java that worked:

try {
            WebDriver driver;
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//chromedriver");
            driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.get("file:///home/eclipse-workspace/demo/test.html");
            Thread.sleep(2000);
            driver.findElement(By.cssSelector("a[title='Nuova Richiesta']")).click();
            
            Thread.sleep(5000);
            driver.close();
            driver.quit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

You can translate this line to Python:

driver.findElement(By.cssSelector("a[title='Nuova Richiesta']")).click();
0

In UI automation testing, there are multiple ways of locating the element of DOM using different attributes. We can refer any of these : Id, class, css, xpath, partial-link, text etc.

Best practices of creating a locator following by any leading software testing services company is to create generic locator so that it won't get affected on any DOM hierarchy related changes.

By considering your element tag and attributes, any of the following locators will find the element and perform required action :

//span[@class='glyphicon glyphicon-plus']/..

//a[@title='Nuova Richiesta']

Further, problem of "unable to click/find element" can be resolved by using the JavaScriptExecutor interface with the following generic code :

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript(Script,Arguments);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.