playwright._impl._api_types.Error: Error: failed to find element matching selector "//*[@id='rc-tabs-1-panel-apikey']/div/span/input"

Viewed 472

I am getting the error message in Playwright as,

playwright._impl._api_types.Error: Error: failed to find element matching selector "//*[@id='rc-tabs-1-panel-apikey']/div/span/input"

I have even tried to change the selectors too but seems like there is some issue with the selectors or something.Please let me know

This is my code

def test_run(playwright):
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
# Open new page
page = context.new_page()
page.goto("website url", wait_until="networkidle")
# Click [data-testid="username"]
page.click("[data-testid=\"username\"]")
# Fill [data-testid="username"]
page.fill("[data-testid=\"username\"]", "abc")
# Click [data-testid="password"]
page.click("[data-testid=\"password\"]")
# Fill [data-testid="password"]
page.fill("[data-testid=\"password\"]", "abc")
# Click [data-testid="loginBtn"]
with page.expect_navigation():
    page.click("[data-testid=\"loginBtn\"]")
# Click on Avatar
page.click("[data-testid=\"avatarContainer\"]")
# Click [data-testid="dropdownSettingsOption"]
page.click("[data-testid=\"dropdownSettingsOption\"]")
time.sleep(2)
# Click text=API Key
page.click("text=API Key")
# Click button:has-text("Regenerate API Key")
time.sleep(2)
page.click("button:has-text(\"Regenerate API Key\")")
time.sleep(2)
# p1 = str(page.text_content("xpath=//*[@id='rc-tabs-1-panel-apikey']/div/span/input"))
p1 = str(page.eval_on_selector("//*[@id='rc-tabs-1-panel-apikey']/div/span/input", "e => e.value"))
print(p1)
context.close()
browser.close()
1 Answers

Type F12 in Google Chrome and go to the console.

Write $x() and place the xpath selector inside the parenthesis. Hit the enter key and make sure you actually find the right selector.

No need to use e.value, you can do something like this:

p1 = page.locator("//*[@id='rc-tabs-1-panel-apikey']/div/span/input")
p1text = p1.inner_text

Also - use * as less as possible. If the element with the id is a div write div for example and not *. It makes the searching faster.

Related