我无法找到元素在其上执行操作
当没有唯一的标识符(例如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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您正在尝试找到
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 PythonBut in your case you probably don't need/want to click this specific
span
, becouse you have custom elemention-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.