python - How to handle google website data security pop up?
问题描述
I am new to Python and to Selenium and was running into a problem using this simple program:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:/Users/Marc/Desktop/Chromedriver/chromedriver.exe")
#open google
driver.get("https://www.google.de")
#search for test
driver.find_elements_by_name("q").send_keys("test")
driver.find_elements_by_name("btnK").click()
It just should open the google webpage in Chrome and initiate a search for the word "test". However when opening the webpage a new element pops up informing about data security and cookies and the search is not executed -> "AttributeError: 'list' object has no attribute 'send_key". (As far as I understand it the reason for the pop up is due to the fact that the browser run by chromedriver doesn't use any of the preexisting cookies.)
How can I solve this? I already tried to us the switch_to_alert method but it didn't work out.
edit: For me it is not about the google website itself but how to deal with it in general as it could occur on other websites as well. Why can't I perform an action on this element? or how can I?
Thanks for your help in advance!
解决方案
如果您在浏览器中签入开发工具,则 Google 网站上的弹出窗口位于 iframe 中。因此,您只需要像这样切换到 iframe 的上下文:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:/Users/Marc/Desktop/Chromedriver/chromedriver.exe")
#open google
driver.get("https://www.google.de")
#search for test
iframe = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(iframe)
elt = driver.find_elements_by_class_name("CwaK9")
elt[2].click()
driver.find_elements_by_name("q")[0].send_keys("test")
driver.find_elements_by_name("btnK").click()
推荐阅读
- java - TableViewer.getElementAt() returning a filtered element
- c - 是否可以将二进制数据嵌入到 Turbo C 制作的 DOS EXE 中?
- c# - 如何重新排列一个列表与另一个列表相同?
- php - 读取csv时如何指定分隔符?
- python - 在 Tensorflow 中训练 while 循环
- java - 会话在springboot项目中过期
- ionic-framework - 如何删除 main.js Ionic 3 中的物理路径?
- mysql - 对于另一个表中的每一行,将一个表中的行写入另一个表
- python - 将 matplotlib 图形转换为相同形状的 numpy 数组
- firebase - 如果 Firestore 对象不存在,则设置它