<h2>Account Management</h2>
<table class="form-table">
<tr id="password" class="user-pass1-wrap">
<th><label for="pass1">New Password</label></th>
<td>
<input class="hidden" value=" " /><!-- #24364 workaround -->
<button type="button" id="wp-generate-pw" class="button button-
secondary wp-generate-pw hide-if-no-js">Generate Password</button>
<div class="wp-pwd hide-if-js">
<span class="password-input-wrapper">
在上面的源代码中,我想找到id为“id ='wp-generate-pw”的按钮。 我试图通过xpath找到
classButtonXpath= "//*[@id='wp-generate-pw']"
siteClassNameElement = self.driver.find_element_by_xpath(classButtonXpath)
siteClassNameElement.click()
但是我得到以下错误:NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“// * [@ id ='wp-generate-pw “]“
我试图以多种方式定位,但仍然无法
有什么想法吗? 感谢
答案 0 :(得分:0)
如果动态生成目标table
元素,您可能需要等到按钮出现在DOM
中:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
wait(self.driver, 10).until(EC.element_to_be_clickable((By.ID, "wp-generate-pw"))).click()
如果此方法也不起作用,您可以检查table
是否位于iframe
内。如果是这样,您应该在点击iframe
:
button
self.driver.switch_to.frame("frameID") # replace "frameID" with actual value
classButtonXpath= "//*[@id='wp-generate-pw']"
siteClassNameElement = self.driver.find_element_by_xpath(classButtonXpath)
siteClassNameElement.click()
self.driver.switch_to.default_content()