无法在Python的Selenium中正确使用XPATH。

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

Unable to use a correct XPATH in Python selenium

问题

I'm getting the correct XPATH from Google Chrome and verified that by doing a DOM search as shown below:
无法在Python的Selenium中正确使用XPATH。

However, I'm getting the following error wherever I try to use this XPATH in Python

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[9]/div[1]/div/div/ul/li[2]/a"}

Any idea what might went wrong?

That's the code part where I'm getting the above error, knowing that the rest of the code works fine:

standard_release_button = self.driver.find_element(By.XPATH, "/html/body/div[9]/div[1]/div/div/ul/li[2]/a")
standard_release_button.click()
英文:

I'm getting the correct XPATH from Google Chrome and verified that by doing a DOM search as shown below:
无法在Python的Selenium中正确使用XPATH。

However, I'm getting the following error wherever I try to use this XPATH in Python

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[9]/div[1]/div/div/ul/li[2]/a"}

Any idea what might went wrong?

That's the code part where I'm getting the above error, knowing that the rest of the code works fine:

    standard_release_button = self.driver.find_element(By.XPATH, "/html/body/div[9]/div[1]/div/div/ul/li[2]/a")
    standard_release_button.click()

答案1

得分: 2

因为该元素位于一个iframe中。虽然我看不到完整的HTML,但我可以看到关闭包含该元素的iframe的</iframe>标签。

在与位于其中的元素进行交互之前,您需要首先切换到该iframe中。关于这方面的文档可以在selenium网站上找到:https://www.selenium.dev/documentation/webdriver/interactions/frames/

例如,如果页面上只有一个iframe(或者它是第一个iframe),您可以使用以下代码首先切换到它:

iframe = driver.find_element(By.CSS_SELECTOR, "iframe")
driver.switch_to.frame(iframe)

然后,您可以使用您现有的代码来点击iframe内部的元素。(根据需要添加等待元素加载的代码,以防元素在尝试点击之前没有立即加载完成。)

英文:

It's because that element is located in an iframe. Although I can't see the full HTML, I can see the &lt;/iframe&gt; tag that closes the iframe that the element is in.

You need to switch into the iframe first before you can interact with elements that are inside it. There's documentation for that on the selenium website: https://www.selenium.dev/documentation/webdriver/interactions/frames/

Eg, if there's only one iframe on that page (or it's the first one), you can switch to it first with this code:

iframe = driver.find_element(By.CSS_SELECTOR, &quot;iframe&quot;)
driver.switch_to.frame(iframe)

and then you can use your existing code to click on the element inside the iframe. (Add element-waiting as needed in case the element doesn't load right away before trying to click on it.)

答案2

得分: 0

当您运行该代码部分时,元素可能尚未加载。
您可以使用WebDriverWait等待元素加载。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

try:
    standard_release_button = WebDriverWait(self.driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "/html/body/div[9]/div[1]/div/div/ul/li[2]/a"))
    )
    standard_release_button.click()
except Exception as e:
    print("Exception: ", e)

它将等待10秒钟,直到元素在DOM中出现。
如果在10秒后仍未加载该元素,将捕获异常并打印异常信息。

英文:

When you run that code part, the element might not have loaded yet.
You can use WebDervierWait to wait for an element to be loaded.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import execpted_condition as EC

try:
    standard_release_button = WebDriverWait(self.driver, 10).until(
    EC.presence_of_element_located((By.XPATH, &quot;/html/body/div[9]/div[1]/div/div/ul/li[2]/a&quot;))
)
    standard_release_button.click()
except Exception as e:
    print(&quot;Exception: &quot;, e)

It will wait 10 seconds for the element to be populated in the DOM
If the element is not loaded after 10 seconds, the exception will be caught and print the exception.

huangapple
  • 本文由 发表于 2023年6月8日 08:22:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427869.html
匿名

发表评论

匿名网友

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

确定