如何解决如何在Selenium中处理Google隐私弹出窗口Python
我对硒很陌生,我进行了很多搜索,但找不到解决我问题的答案。 我想打开Firefox,在Google上搜索。将所有内容存储在列表中并将其打印到我的控制台。
但是,每次我用硒打开Firefox时,都会打开一个弹出窗口,其中包含对Google隐私权规定的确认。我不知道如何关闭它。
这是我尝试过的:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
# Navigate to the application home page
driver.get("http://www.google.com")
main_page = driver.current_window_handle
time.sleep(3)
#Tried to find out in which browers window I am right now -> terminal says '19'
print('The actual browers window is: {}'.format(main_page))
driver.find_element_by_xpath("//*[@id='introAgreeButton']/span/span").click()
# get the search textbox
search_field = driver.find_element_by_id("lst-ib")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("Flowers")
search_field.submit()
# get the list of elements which are displayed after the search
# currently on result page using find_elements_by_class_name method
lists= driver.find_elements_by_class_name("_Rm")
# get the number of elements found
print ("Found " + str(len(lists)) + " searches:")
# iterate through each element and print the text that is
# name of the search
i=0
for listitem in lists:
print (listitem.get_attribute("innerHTML"))
i=i+1
if(i>10):
break
# close the browser window
driver.quit()
解决方法
iframe中包含Google隐私弹出窗口。您必须切换到iframe,然后寻找“接受”(或“拒绝”)按钮。像这样
wait = WebDriverWait(driver,5)
iframe = wait.until(EC.element_to_be_clickable([By.CSS_SELECTOR,'#cnsw > iframe']))
driver.switch_to.frame(iframe)
然后在iframe中查找“接受”按钮,然后单击它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。