0
votes

I am new to HTML and Selenium, and I have struggling with the following error:

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//[contains(text(),'Manage Users')]"}*

I have tried a wide array of various combinations of attributes, and to no avail. I can't directly use the ID of the element because it is dynamic. The code below is a fraction of the combinations that I have tried:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome()
driver.get('url')

driver.find_element_by_id('j_username').send_keys('my_username')
driver.find_element_by_id('j_password').send_keys('my_password')
driver.find_element_by_id('button.ok').click()

time.sleep(3)

driver.switch_to.frame('portfolio_canvas_area')
driver.switch_to.frame('portfolio_area')

time.sleep(5)

#driver.find_element_by_xpath("/html/body/div/div/form/span/span[1]/span/div/div/table[2]/tbody/tr[3]/td/table/tbody/tr/td[2]/span/span/a")
driver.find_element_by_xpath("//*[contains(text(),'Manage Users')]")
#WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div/div/form/span/span[1]/span/div/div/table[2]/tbody/tr[3]/td/table/tbody/tr/td[2]/span/span/a'))).click()
#WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//span[text()="Manage Users"]'))).click()

The following is the HTML in question giving me issues:

<a href="javascript:action('https://website.com/itim/console/action/Te502a9571b590da5d71c7#W5f82fbdc1b590c3ea782a_treeSel(3)')" id="W5f82fbdc1b590c3ea782a_treeSel(3)" name="W5f82fbdc1b590c3ea782a_treeSel(3)" onclick="if (confirmPageChange()) {saveTaskFormElements();return doSubmit('form', 'W5f82fbdc1b590c3ea782a_treeSel', 'treeSel(3)', 'W5f82fbdc1b590c3ea782a_treeSel(3)', 'treeFunc', 'wh', 'wa'); }" style="color:#000000;font-size:83.3%;font-family:Arial, Helvetica, sans-serif;" taskid="MANAGE_USERS" title="Manage Users">Manage Users</a>

I have also attached some pictures of parent HTML code from Chrome's Inspector with this question. I would greatly appreciate your help in relieving some of the headaches that this has caused! Thank you!

enter image description here enter image description here The following picture shows the webpage, where buttons on the left frame opens up a new tab displayed on the right frame. enter image description here

1
There might be a 3rd frame taskentriesframe. - Arundeep Chohan
@ArundeepChohan That's exactly it! Thanks so much, I totally missed it! - Daniel Zhou

1 Answers

0
votes

The message indicates that selenium could not locate the element.

So maybe you can try using the following example, I use it all the time to avoid errors like this.

driver.find_element(By.XPATH, "//*[text()='Manage Users']").click()

if that doesn't work, here is a list of things you can try

# 1
driver.implicitly_wait(15)

# 2
# make sure the text "Manage Users" don't have space before or after in the HTML code

# 3
# It seems that the "Manage Users" button is a link so you can try the link method
driver.find_element_by_link_text("Manage Users").click()  

# 4
# You can also try with ActionChains, need to import the module first
from selenium.webdriver.common.action_chains import ActionChains  

# then you have try the following
actions = ActionChains(driver)
element = driver.find_element(By.XPATH, "//*[text()='Manage Users']")
actions.move_to_element(element).click(on_element=element).perform()

Hopes this helped!