在Selenium中找不到网站元素时遇到问题。

huangapple go评论66阅读模式
英文:

Having issues finding an website element in Selenium

问题

I'm building a bookerbot for fun, and it's my first time using Selenium. I'm really stumped for why I keep getting the following error:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id=\"root\"]/main/section[2]/div/button"}

The sample website I'm trying this is a workout class site: https://www.barrys.com/my-account/

I'm simply trying to click on the login button, so using the following command:

WebDriverWait(driver, 5000).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/main/section[2]/div/button'))
    ).click()
    driver.find_element(By.XPATH, '//*[@id="root"]/main/section[2]/div/button')
英文:

I'm building a bookerbot for fun, and it's my first time using Selenium. I'm really stumped for why I keep getting the following error:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="root"]/main/section[2]/div/button"}

The sample website I'm trying this is a workout class site: https://www.barrys.com/my-account/

I'm simply trying to click on the login button, so using the following command:

WebDriverWait(driver, 5000).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/main/section[2]/div/button'))
    ).click()
    driver.find_element(By.XPATH, '//*[@id="root"]/main/section[2]/div/button')

答案1

得分: 0

There is an iframe within which the Login button exists. So, before clicking on the login button, you need to switch into the iframe, then perform the action. Try the below code:

iframe = driver.find_element(By.XPATH, '//iframe[1]')
driver.switch_to.frame(iframe)
WebDriverWait(driver, 5000).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="root"]/main/section[2]/div/button'))
).click()
英文:

There is an iframe within which the <kbd>Login</kbd> button exist. So, before clicking on the login button, you need to switch into the iframe, then perform the action. Try the below code:

iframe = driver.find_element(By.XPATH, &#39;//iframe[1]&#39;)
driver.switch_to.frame(iframe)
WebDriverWait(driver, 5000).until(
    EC.element_to_be_clickable((By.XPATH, &#39;//*[@id=&quot;root&quot;]/main/section[2]/div/button&#39;))
).click()

huangapple
  • 本文由 发表于 2023年2月24日 15:25:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定