How to click an element with Selenium webdriver javascript? - node.js

I am testing, and I have to click on elements, but it sometimes click, sometimes not. I tried some solutions, but there is no one, which always clicks.
Is there any solution to click an element, which always works?
I tried these codes:
1:
await driver.findElement(By.id("u_0_a")).click();
2:
var element = await driver.findElement(By.id("u_0_a"));
await element.click();
3:
await driver.executeScript("document.getElementById('u_0_a').click()");
I use selenium with node.js, javascript, chrome driver.

Try using:
var element = await driver.findElement(By.id("u_0_a"));
await driver.executeScript("arguments[0].click();", element)
Hope this helps!

Try using TryUntil. I hope this works. The below code is in C#
Browser.TryUntil(() => { }, () => Browser.FindElement(By.id("u_0_a")).Exists(), TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(120));
Browser.FindElement(By.id("u_0_a")).Click();

Thank you for your help. The problem was not in my code. The problem was with the https://www.facebook.com/ .
Because facebook, is changing the ID, and sometimes it was the login button sometimes a gender custom button. :/

Please try sleep before you handle the click operation.
Example as below:
await driver.sleep(2000)
await driver.findElement(By.xpath('element path')).click()

Related

How to click a link label within an unordered list

I'm fairly new to puppeteer and have been assigned the task to automate the process of report generation from a CRM. One of the steps involve clicking on this link within a list. I've tried doing this with the following two methods, but none have worked.
The two ways I have tried:
Simulate Keyboard keystrokes (CTRL + ALT + T)
await page.keyboard.down('T');
await page.keyboard.down('Alt');
await page.keyboard.down('Control');
await page.keyboard.up('T', {delay:250});
await page.keyboard.up('Alt', {delay:250});
await page.keyboard.up('Control', {delay:250});
Click the link within the list
const sizeButton = await page.waitForXPath(`//[#id="md86fe08f_ns_menu"]`);
await sizeButton.evaluate(btn => {btn.closest("li").dispatchEvent(new Event("mousedown"));});
This is the code of the list (using the inspect element). Any help would be appreciated.

How to click element in Puppeteer using xPath

I want to click on an element without using css selectors.
await page.click()
uses selectors to identify the element.
So how can I do something like this?
await page.click('/*[#id="toc"]/ul/li[1]/a')
First I had to get the element by using
await page.$x('<xPath>')
It returns an array with elements. To click I had to choose the first element in the array.
const elements = await page.$x('<xPath>')
await elements[0].click()
You can use the xpath prefix with puppeteer 19 and newer
await page.click('xpath/' + xpathExpression)
e.g.
await page.click('xpath//*[#id="toc"]/ul/li[1]/a')

Use puppeteer to search for element based on inner text

I've been attempting to do some web scraping using puppeteer, and I've run into the following problem: I want to click an element that has a specific inner text (in this case 'INHERITANCE TAX RETURN'), but everything else about the element seems to be identical to a lot of other elements on the page. I was wondering if anyone knew a way to search for an element based on its inner text. Any help would be greatly appreciated.
Have you tried:
const linkHandlers = await page.$x("//span[contains(text(), 'INHERITANCE TAX RETURN')]");
if (linkHandlers.length > 0) {
await linkHandlers[0].click();
} else {
throw new Error("Link not found");
}

Element is not currently visible and so may not be interacted with node and selenium driver

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();
}
});
});
});
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().
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.

In NodeJS selenium-webdriver, how do you get innerHTML of element

I've gone through: https://code.google.com/p/selenium/wiki/WebDriverJs and it doesn't have any information. So, can someone help.
I have
var element = driver.findElements(webdriver.By.id("something"))
console.log('text='+element.getAttribute("inner‌​HTML"));
But doesn't work. Most of the documentation appears to be for JAVA not nodeJS. If you come across a .getText() function, I'm pretty sure that is JAVA. I actually just want the text part innerText, opposed to innerHTML. But that might be asking too much.
You can check innerHTML like this:
driver.executeScript(function() {
return document.querySelector('#something').innerHTML;
}).then(function(innerHTML) {
//check content here
});
Per the webdriver.js docs:
var myElement = element(by.css('.myclass'));
myElement.getInnerHtml().then(function(html) {
//do stuff with html here
});
Hope that helps! It's working for me using Node.js + Selenium / WebDriver, etc.

Resources