0

I have the following code and I cannot get the driver to click the div. It keeps throwing the error

"Element is not currently visible and so may not be interacted"

when debugging you can clearly see that the element is visible. How can I ignore the warning or the error?

var webdriver = require('selenium-webdriver')
    , By = webdriver.By
    , until = webdriver.until;
var driver = new webdriver.Builder().forBrowser('firefox').build();
driver.get('http://www.vapeworld.com/');
driver.manage().timeouts().implicitlyWait(10, 10000);
var hrefs = driver.findElements(webdriver.By.tagName("a"));
hrefs.then(function (elements) {
    elements.forEach(function (element) {
        element.getAttribute('name').then(function (obj) {
            if (obj == '1_name') {
                console.log(obj);
                element.click();
            }
        });
    });
});
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Yelitza
  • 23
  • 3
  • If you can share your html, I can help you better. But I think that it might be a frame and you should switch to the frame before clicking on it. Check this: http://toolsqa.com/selenium-webdriver/handling-iframes-using-selenium-webdriver/ If it helps you, let me know to post it as an answer. – Jose Jul 01 '16 at 06:48
  • The website page html is in the code I posted in the question. – Yelitza Jul 01 '16 at 14:56
  • In your question you stated that you wanted to "click the div." Which of the 278 divs on the page did you want to click? Please read the help topics on how to ask a good question then edit your question to have some more detail and clarity on what you are asking. – JeffC Jul 02 '16 at 16:32

2 Answers2

0

Your code is clicking an A tag with the name "1_name". I'm looking at the page right now and that element doesn't exist, hidden or otherwise.

You'd be better served by replacing the bulk of your code with a CSS selector, "a[name='1_name']" or "a[name='" + tagName + "']", that will find the element you want with a single find. You can then click on that element.

The issue you are running into is that the element you are trying to click is not visible, thus the error message. Selenium is designed to only interact with elements that the user can see, which would be visible elements. You will need to find the element you are looking for and figure out how to make it visible. It may be clicking another link on the page or scrolling a panel over, etc.

If you don't care about user scenarios and just want to click the element, visible or not, look into .executeScript().

JeffC
  • 22,180
  • 5
  • 32
  • 55
-1

Looked at the website and used the F12 tool (Chrome) to investigate the page:

var elements = [].slice.call(document.getElementsByTagName("a"));
var elementNames = elements.map(function (x) { return x.getAttribute("name"); });
var filledElementNames = elementNames.filter(function (x) { return x != null; });
console.log(filledElementNames);

The content of the website http://www.vapeworld.com is very dynamic. Depending on the situation you get one or more anchors with "x_name" and not always "1_name": the output of the script in Chrome was ["2_name"] and Edge returns ["1_name", "9_name", "10_name", "17_name", "2_name"]. So "you can clearly see that the element is visible" is not true in all situations. Also there were some driver bugs on this subject so it is worthwhile to update the driver if needed. See also the answers in this SO question explaining all the criteria the driver uses. If you want to ignore this error you can catch this exception:

try {
  element.click();
} catch (Exception ex) {
  console.log("Error!");   
}

See this documentation page for more explanation.

Community
  • 1
  • 1
Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32
  • The issue is not finding the element. The listed example finds the element but cannot invoke a click element.click(); "Element is not currently visible and so may not be interacted" – CodeMilian Jul 01 '16 at 22:16