简体   繁体   English

我正在尝试使用 selenium python 在 chrome 新选项卡中单击一个元素,但我收到错误 no such element 即使该元素存在

[英]I am trying to use selenium python to click an element in chrome new tab, but I am getting error no such element even thought the element is there

I got all the right modules that I need and my code looks pretty good.I am trying to click the add shortcut button using selenium, it is my first time using selenium, but I am pretty sure I did all of the code right.我得到了我需要的所有正确模块,我的代码看起来很不错。我正在尝试使用 selenium 单击添加快捷方式按钮,这是我第一次使用 selenium,但我很确定我做对了所有代码。 This is all of my code:这是我的所有代码:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get('chrome://newtab')
add_button = driver.find_element_by_id('addShortcut')
add_button.click()

This is the chrome elements: https://i.stack.imgur.com/m0DJR.png This is my error:这是镀铬元素: https://i.stack.imgur.com/m0DJR.png这是我的错误:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\Srikar's Stuff\Programming\Python\WebScraper.py", line 7, in <module>
    add_button = driver.find_element_by_id('addShortcut')
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="addShortcut"]"}
  (Session info: chrome=89.0.4389.90)

Is it an error with my code or something else?是我的代码错误还是其他错误? Help????帮助????

The icons aren't on that page.图标不在该页面上。 They're in an <iframe> called chrome-untrusted://new-tab-page/custom_background_image?url= , and I don't think you can bring that up on its own.它们位于一个名为chrome-untrusted://new-tab-page/custom_background_image?url=<iframe>中,我认为您不能单独提出它。 Why would you want to?你为什么想要? Unless you login, this Chrome isn't connected to your desktop Chrome.除非您登录,否则此 Chrome 不会连接到您的桌面 Chrome。

So Google is using the feature of Shadow Root .所以谷歌正在使用Shadow Root的功能。 It hides what is under it as an Iframe.它将下面的内容隐藏为 Iframe。 I found the following workaround:我找到了以下解决方法:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get('chrome://newtab')
delay = 1 # seconds
js_query = "return document.getElementsByTagName('ntp-app')[0].shadowRoot.getElementById('mostVisited').shadowRoot"
try:
    _ = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.TAG_NAME, 'ntp-app')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")
    
most_visited = driver.execute_script(js_query)
add_button = most_visited.find_element_by_id('addShortcut')
add_button.click()

This should work though it is a little bit hacky这应该可以工作,尽管它有点 hacky

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Selenium:我正在获取元素未附加到页面文档错误 - Python Selenium: I am getting element is not attached to the page document error 我正在尝试使用 selenium 加载等待列表并单击按钮,但我似乎找不到该元素 - I am trying to use selenium to load a waiting list and click the button, but I can't seem to find the element 为什么即使元素存在于我的循环中,我也会收到 StaleElementReferenceException? (硒蟒) - Why am I getting StaleElementReferenceException even though the element is present in my loop? (Selenium Python) 为什么在 python flask 中使用 Selenium 时出现 No such element 异常? - Why am I getting a No such element exception using Selenium in python flask? 为什么在尝试使用 selenium 搜索栏 select 时出现“元素不可交互”错误? - Why am I getting an "element not interactable" error when trying to select a search bar using selenium? 我收到此错误找不到元素 - I am getting this error cannot find element 我试图遍历列中的每个元素并单击它,但它给了我错误。 “找不到元素” - I am trying to iterate on each element in column and click on it, but it gives me error. "can not locate the element" 我是 python 新手,在打印列表元素时遇到问题 - I am new to python and am having trouble printing an element of a list 为什么我在页面上找到了StaleElementReferenceException,但仍然找到了该元素? - Why am I getting a StaleElementReferenceException even though it found the element on the page? 为什么我无法使用 Python 和 Selenium 找到 html 元素? - Why am I unable to find html element with Python and Selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM