英文:
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:
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:
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 </iframe>
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, "iframe")
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, "/html/body/div[9]/div[1]/div/div/ul/li[2]/a"))
)
standard_release_button.click()
except Exception as e:
print("Exception: ", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论