Ian Richards
Ian Richards

Reputation: 1618

A way of clicking on hidden elements in protractor end to end tests

Is there a way to click on a hidden value in a sub menu. I would like to be able to do something like

driver.findElement(protractor.By.xpath('/html/body/div/div/a')).mouseover.then(function() {
    ptor.findElement(protractor.By.className('name').getText().then(function(result) {
        expect(result).toBe('Me');
    });
});

when the menu item is not visible, or are we limited with this at the moment. If this is not possible is there a way around this issue at present.

Upvotes: 9

Views: 10910

Answers (2)

Arutsudar Arut
Arutsudar Arut

Reputation: 513

Just to add on to the accepted answer, the below code worked for me.

Import statement:

import { browser, by, element } from 'protractor';

Code:

const hiddenElement = element(by.id('hiddenIcon'));
browser.driver.executeScript('arguments[0].click();', hiddenElement.getWebElement());

Just change the hiddenIcon to the id of your element.

Instead of using

var ptor = protractor.getInstance(); var driver = ptor.driver;

we can also use

browser.driver

Upvotes: 0

Ian Richards
Ian Richards

Reputation: 1618

ok so after a long and painful search trying to find an answer to this question I finally came across the answer trying to answer a different question.

Most of the documentation I found explain that we must use Actions in the form of a WebElement and then cast that to Javascript and pass it a script element in the form of an array with the click action.

Well the same kinds goes here but with a few modifications.

describe('', function() {
    var ptor = protractor.getInstance();
    var driver = ptor.driver;

    it('', function() {
        var hiddenElement = driver.findElement(protractor.By.yourchosenlocator(''));
        driver.executeScript("arguments[0].click()", hiddenElement).then(function() {
            expect(whatever).toMatch(whatever);
        });
    }, 30000);
});

as you can see there is no use of webelement and no cast required.

Here are the sources that helped me in my search for answers

How do you click on an element which is hidden using Selenium Webdriver?

SELENIUM WEBDRIVER – HOW TO CLICK ON A HIDDEN LINK OR MENU

Selenium WebDriver - hidden select and anchor [duplicate]

Upvotes: 11

Related Questions