[英]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.