我不熟悉HTML和Selenium,我遇到了以下错误:
引发异常\u类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“/[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代码的图片和这个问题。我将非常感谢您的帮助,以减轻一些头痛,这已经造成了!谢谢大家!
该消息表示selenium找不到该元素
所以,也许你可以尝试使用下面的例子,我一直使用它来避免类似这样的错误
如果这不起作用,这里列出了一些你可以尝试的事情
希望这有帮助
相关问题 更多 >
编程相关推荐