Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Quality Assurance Introduction to Selenium Automation Nation A Fork in the XPath

FU SOON LEE
FU SOON LEE
9,413 Points

I got Unable to locate element error

[13380:6340:0121/202859.228:ERROR:process_metrics.cc(105)] NOT IMPLEMENTED DevTools listening on ws://127.0.0.1:12722/devtools/browser/3a6f7829-a17a-4a81-ba3c-23b82e057357 (node:4620) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//span[text()='Shadd Anderson']/../button[last()]"}

FU SOON LEE
FU SOON LEE
9,413 Points

require("chromedriver");

const selenium = require("selenium-webdriver"); const By = selenium.By; const driver = new selenium.Builder().forBrowser("chrome").build();

const url = "http://port-80-rpf3l24it5.treehouse-app.com/";

const invitees = [ 'Gonzalo Torres del Fierro', 'Shadd Anderson', 'George Aparece', 'Shadab Khan', 'Joseph Michael Casey', 'Jennifer Nordell', 'Faisal Albinali', 'Taron Foxworth', 'David Riesz', 'Maicej Torbus', 'Martin Luckett', 'Joel Bardsley', 'Reuben Varzea', 'Ken Alger', 'Amrit Pandey', 'Rafal Rudzinski', 'Brian Lynch', 'Lupe Camacho', 'Luke Fiji', 'Sean Christensen', 'Philip Graf', 'Mike Norman', 'Michael Hulet', 'Brent Suggs' ]; driver.get(url);

const locator = { inviteForm: By.id("registrar"),

//inviteeNameField: By.name("name") inviteeNameField : By.css("#registrar input[name=\"name\"]"), toggleNonRespondersVisibility : By.css(".main >div input"), removeButton : By.css("#invitedList li button + button"), //removeButtonForInvitee : By.css("#invitedList li button + button") //removeButtonForInvitee : By.xpath("//li/button[2]") };

function removeInvitee(){ driver.findElement(locator.removeButton) .click(); }

function addInvitee(name){ driver.findElement(locator.inviteeNameField) .sendKeys(name); driver.findElement(locator.inviteForm).submit();

}

function toggleNonRespondersVisibility(){ driver.findElement(locator.toggleNonRespondersVisibility) .click(); }

invitees.forEach(invitee => addInvitee(invitee));

removeInvitee(); //toggleNonRespondersVisibility();

Mariana Belko Nicholas
Mariana Belko Nicholas
12,964 Points

I came across the same problem and realized I wasn't using Selenium v3. After installing the correct version, my code worked!

5 Answers

Hoessein Abd
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Hoessein Abd
Python Web Development Techdegree Graduate 19,107 Points

Can you provide the html?

From looking at the error message, selenium driver can't find the element.

I'm assuming here but try to add last-child like this [last-child()] instead of just 'last' to locate the button.

FU SOON LEE
FU SOON LEE
9,413 Points

I am following exactly the same as per the video, but it show me unable to locate the element. I tried using direct css and xpath, both giving me the same problem. However, the script work fine if I run with my command prompt. It just not working with Visual code. I tried using all these method below, but not working.

removeButton : By.css("#invitedList li button + button"),

//removeButtonForInvitee : By.css("#invitedList li button + button")

//removeButtonForInvitee : By.xpath("//li/button[2]")

//removeButtonForInvitee : By.xpath("//span[name='Shadd Anderson']/../button[2]");

Craig Dennis Nathan Menge I am also facing the same problem as FU SOON LEE.

Here is my code.

require("chromedriver")
const selenium = require("selenium-webdriver");
const By = selenium.By;

const driver = new selenium.Builder()
    .forBrowser("chrome")
    .build();

//driver.get(process.env.URL);
driver.navigate().to("http://port-80-irmajcjpef.treehouse-app.com/");

const locators = {
    inviteeForm: By.id("registrar"),
    inviteeNameField: By.css("#registrar input[name ='name']"),
    togglerescheckbox: By.css(".main >div input"),
    removebuttonForinvitees: invitee => By.xpath(`//span [text() = "${invitee}"]/../button[text() = "remove"]`)
};

const invitees = [
    'Mohammed Ismail',
    'Mohammed Ibrahim',
    'Mohammed Arafat',
    'Abdul Bari',
    'Yahya',
    'Mohammed Iqbal'
 ];

function addInvitee(name) {
    driver.findElement(locators.inviteeNameField)
        .sendKeys(name);
    driver.findElement(locators.inviteeForm).submit();
}

function removeInvitee(invitee) {
    driver.findElement(locators.removebuttonForinvitees(invitee))
    .click();
}

function toggleres(){
    driver.findElement(locators.togglerescheckbox)
    .click();
}

invitees.forEach(addInvitee);

removeInvitee("Mohammed Ismail");

//toggleres();

Can you please help me on this - I am stuck on this for days and need a way out.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Try removing the space from here

//span [

Craig Dennis I did try removing spaces. I am still getting below error message!

[2640:19016:0501/114712.335:ERROR:configuration_policy_handler_list.cc(92)] Unknown policy: AlwaysAuthorizePlugins [2640:19016:0501/114712.337:ERROR:configuration_policy_handler_list.cc(92)] Unknown policy: DisablePluginFinder [2640:19016:0501/114712.338:ERROR:configuration_policy_handler_list.cc(92)] Unknown policy: PasswordManagerAllowShowPasswords [2640:19016:0501/114712.339:ERROR:configuration_policy_handler_list.cc(92)] Unknown policy: DnsPrefetchingEnabled [2640:19016:0501/114712.442:ERROR:configuration_policy_handler_list.cc(92)] Unknown policy: AlwaysAuthorizePlugins [2640:19016:0501/114712.444:ERROR:configuration_policy_handler_list.cc(92)] Unknown policy: DisablePluginFinder [2640:19016:0501/114712.444:ERROR:configuration_policy_handler_list.cc(92)] Unknown policy: PasswordManagerAllowShowPasswords [2640:19016:0501/114712.445:ERROR:configuration_policy_handler_list.cc(92)] Unknown policy: DnsPrefetchingEnabled

DevTools listening on ws://127.0.0.1:12489/devtools/browser/2392695b-d7ea-40af-a1a0-adb32e30e399 (node:3212) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//span [text() = "Mohammed Ibrahim"]/../button[2]"} (Session info: chrome=65.0.3325.181) (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.15063 x86_64) at Object.checkLegacyResponse (C:\Users\mohammedi472\selenium-basics\node_modules\selenium-webdriver\lib\error.js:585:15) at parseHttpResponse (C:\Users\mohammedi472\selenium-basics\node_modules\selenium-webdriver\lib\http.js:533:13) at Executor.execute (C:\Users\mohammedi472\selenium-basics\node_modules\selenium-webdriver\lib\http.js:468:26) at <anonymous> at process._tickCallback (internal/process/next_tick.js:118:7) (node:3212) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:3212) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Interesting note here:

"selector":"//span [text() = "Mohammed Ibrahim"]/../button[2]"

There is a space between the span and the [text().

Not sure what that top spew is either...

Dylan Meares
Dylan Meares
6,323 Points

I'm getting the same error. No matter what I try it cannot locate that element. Same thing with the edit buttons.