我无法找到元素在其上执行操作

发布于 2025-01-21 09:45:26 字数 1009 浏览 0 评论 0原文

当没有唯一的标识符(例如ID,文本等)时,我正在面临一个问题,在屏幕上找到元素。在打开URL时,我需要向下滚动并单击按钮 - “入门”继续进行!...

下面是我的代码:

global driver
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
    
driver.get("https://My URL")
driver.implicitly_wait(10)

screen = driver.find_element(By.XPATH, '//div[@class="swiper-wrapper"]')
screen.click()  (- This step doesnt through any error, i tried using this to scroll down the screen)

element = driver.find_element(By.XPATH, '//span[contains(text(),"Get Started")]')
driver.execute_script("arguments[0].scrollIntoView(true);", element )
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[contains(text(),"Get Started")]'))).click()
or 
element.click()

请帮助我确定如何找到元素。

“

在这里输入图像说明

I'm facing an issue locating the element on screen when there are no unique identifiers like ID, text etc. As it opens URL, I need to scroll down and click on button - 'Get Started' to proceed!...

Below is my code:

global driver
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
    
driver.get("https://My URL")
driver.implicitly_wait(10)

screen = driver.find_element(By.XPATH, '//div[@class="swiper-wrapper"]')
screen.click()  (- This step doesnt through any error, i tried using this to scroll down the screen)

element = driver.find_element(By.XPATH, '//span[contains(text(),"Get Started")]')
driver.execute_script("arguments[0].scrollIntoView(true);", element )
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[contains(text(),"Get Started")]'))).click()
or 
element.click()

Please help me in determining how to locate the element.

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2025-01-28 09:45:26

在这种情况下,您正在尝试找到span ##Shadow-root,Selenium将无法单击#Shadow-root 以简单的方式。在这里查看: #使用Selenium和Python Shadow-Root(封闭),

但在您的情况下,您可能不需要/需要单击此特定span,因为您有自定义元素ion- button,其中包含一些单击事件侦听器。

您可以在浏览器DevTools中检查XPath值。如果转到元素选项卡,则可以右键单击所需的元素,然后选择“选项”副本> copy xpath。另外,请确保您的元素不在iFrame标签内部,也可能导致问题。

In this case you are trying to find span which is inside #shadow-root, Selenium won't be able to click elements inside #shadow-root in easy way. Look here: How to click button inside #shadow-root (closed) using Selenium and Python

But in your case you probably don't need/want to click this specific span, becouse you have custom element ion-button, which has some click event listener attached to it.

You can check XPATH value in browser devtools. If you go to Elements tab, you can right click desired element and choose option Copy>Copy Xpath. Also make sure your element is not inside iframe tag, it could cause problem as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文