本文介绍了如何使用Selenium/python编辑Chrome搜索和首页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用selenium/python编辑chrome浏览器的搜索和首页.导航到chrome://settings/searchEngines并将目标ID为'addSearchEngine'的'add'按钮定位后,运行.click函数时出现错误.如何正确定位此元素,或者还有另一种方法可以使用python更新Chrome搜索/起始页?

我猜该元素被困在iframe中,但是我无法使用开发工具在页面上找到一个元素,xpath指出了有关绝对xpath的以下内容:它可能是来自不同src的iframe的子元素&目前不支持."

从硒导入Webdriver的

 驱动程序= webdriver.Chrome()driver.set_page_load_timeout(10)driver.get("chrome://settings/searchEngines")driver.find_element_by_id("addSearchEngine").click()追溯(最近一次通话):< module>中的文件"C:/Users/Jonathan/PycharmProjects/test_project/test_project/Main.py",第20行.driver.find_element_by_id("addSearchEngine").click()在find_element_by_id的第360行中,文件"C:\ Python37-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py"返回self.find_element(by = By.ID,value = id_)文件"C:\ Python37-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py",行978,在find_element中'value':value})['value']执行中的文件"C:\ Python37-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py",第321行self.error_handler.check_response(响应)文件"C:\ Python37-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py",第242行,在check_response中引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"css选择器","selector":"[id =" addSearchEngine]"}(会话信息:chrome = 75.0.3770.142) 
解决方案

请参阅

I am trying to edit chrome browsers search and homepage using selenium/python. After navigating to chrome://settings/searchEngines and targeting the 'add' button with the ID 'addSearchEngine', I get an error when I run a .click function. How do I target this element correctly, or is there another way to update chromes search/startpage with python?

I'm guessing this element is trapped inside an iframe but I'm unable to find one on the page using the dev tools, xpath noted the following about the absolute xpath: "It might be a child of iframe from different src & it is not supported currently."

from selenium import webdriver

driver = webdriver.Chrome()
driver.set_page_load_timeout(10)
driver.get("chrome://settings/searchEngines")
driver.find_element_by_id("addSearchEngine").click()

Traceback (most recent call last):
  File "C:/Users/Jonathan/PycharmProjects/test_project/test_project/Main.py", line 20, in <module>
    driver.find_element_by_id("addSearchEngine").click()
  File "C:\Python37-32\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:\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python37-32\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="addSearchEngine"]"}
  (Session info: chrome=75.0.3770.142)
解决方案

Refer to this post for detailed explanation.

In your case you can do the below.

url = "chrome://settings/searchEngines"
driver.get(url)
addButton = driver.execute_script("return document.querySelector('settings-ui')"
                              ".shadowRoot.querySelector('#main')"
                              ".shadowRoot.querySelector('settings-basic-page.showing-subpage')"
                              ".shadowRoot.querySelector('settings-search-page')"
                              ".shadowRoot.querySelector('settings-search-engines-page')"
                              ".shadowRoot.querySelector('#addSearchEngine')")
addButton.click()

Screenshot:

这篇关于如何使用Selenium/python编辑Chrome搜索和首页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:33