Python Selenium:NoSuchElementException:消息:没有这样的元素:无法定位元素

时间:2021-03-03 03:02:47

标签: python html selenium selenium-webdriver selenium-chromedriver

我是 HTML 和 Selenium 的新手,但遇到了以下错误:

引发异常类(消息,屏幕,堆栈跟踪) selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{"method":"xpath","selector":"//[contains(text(),'Manage Users' )]"}*

我尝试了多种不同的属性组合,但都无济于事。我不能直接使用元素的 ID,因为它是动态的。下面的代码是我尝试过的组合的一小部分:

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()

以下是有问题的 HTML,给我带来了问题:

<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>

我还在这个问题中附上了一些来自 Chrome Inspector 的父 HTML 代码的图片。我将非常感谢您的帮助,以减轻由此引起的一些头痛!谢谢!

enter image description here enter image description here 下图显示了网页,其中左侧框架上的按钮会打开一个显示在右侧框架上的新选项卡。 enter image description here

1 个答案:

答案 0 :(得分:0)

该消息表明 selenium 无法定位该元素。

所以也许您可以尝试使用以下示例,我一直使用它来避免此类错误。

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

如果这不起作用,这里是您可以尝试的列表

# 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()

希望这有帮助!

相关问题