0
votes
<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">

In the source code above, i want to locate the button that has id "id='wp-generate-pw". I tried to locate by xpath

classButtonXpath= "//*[@id='wp-generate-pw']"
siteClassNameElement = self.driver.find_element_by_xpath(classButtonXpath)
siteClassNameElement.click()

But I get the following error: NoSuchElementException: Message: no such element: Unable to locate element : {"method":"xpath","selector":"//*[@id='wp-generate-pw']"

I tried to locate in many ways, but still i cant

Any ideas? Thanks

1

1 Answers

0
votes

If target table element generated dynamically you might need to wait until your button appears in 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()

If this approach also doesn't work, you can check whether table located inside an iframe. If so, you should switch to iframe before clicking the 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()