0

I have developed an application using Angular, Node.js/Express.js and MySQL. The application has a login page that is displayed when any user visits the link. After logging in, the user is taken to the home page with a lot of other pages that are displayed in the navbar. I am trying to perform an automation test on the application and I am getting this error when I try to make my automated test click on one of the links in the navbar.

TypeError: 'WebElement' object is not subscriptable

After trying different things the error still does not get resolved. I ended up getting an error of Message: element not interactable

I have found the element using the console in Chrome and it is also clickable when I perform the click action in the console $x("//a")[1].click() or $$("[class = 'navbar-nav ml-auto'] li a")[1].click() but when I run the script in selenium then it throws the error. Can anybody tell me how can I make the navbar item clickable in selenium?

Here's my code:

from selenium import webdriver

browser = webdriver.Chrome()

browser.get('http://name-of-the-local-server:3000')

browser.implicitly_wait(3);

email_element = browser.find_element_by_css_selector("input[name = 'usermail']")
password_element = browser.find_element_by_css_selector("input[name = 'passcode']")
submit_btn_element = browser.find_element_by_css_selector("button[name = 'loginButton']")

email_element.send_keys("[email protected]")
password_element.send_keys("pass123")
submit_btn_element.click()

#executes perfectly until here

navbar_element = browser.find_elements_by_css_selector("[class = 'navbar-nav ml-auto'] li a")
navbar_element[1].click()

EDIT: I have added a bit of HTML that I want to click as by default it loads the home page

 <a _ngcontent-imt-c24="" routerlinkactive="active" routerlink="/admin" 
 class="nav-link" href="/admin"><i _ngcontent-imt-c24="" class="bi bi-people" 
 style="font-size: 1.3rem;"></i>&nbsp; Admin</a>

<i _ngcontent-sun-c24="" class="bi bi-people" style="font-size: 1.3rem;"></i> 

Any help would be appreciated. Thanks!

2
  • Please include the full error Traceback. Commented May 25, 2021 at 7:40
  • This means that you have attached an index to a WebElement which is not supported. Commented May 25, 2021 at 7:43

1 Answer 1

1

Instead of this :

navbar_element = browser.find_element_by_xpath("//a")

use this

navbar_element = browser.find_elements_by_xpath("//a")

find_element will return a single web element where as find_elements will return list of web elements

You are using navbar_element[0].click() if stored in list, it will click on first web element.

or iterate over a list like this :

for link in navbar_element: 
    ActionChains(driver).move_to_element(link).click().perform()
Sign up to request clarification or add additional context in comments.

9 Comments

it's still not working. I am getting an error of Message: element not interactable
@node_modules : Can you share the URL if it is public ?
sorry, it's not public.
Yeah, i changed that to the first index. My typo.
@node_modules : updated the code in the end
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.