兄弟们你们用selenium写网页自动化的时候 弹出新窗口怎么解决的呀
兄弟们你们用selenium写网页自动化的时候 弹出新窗口怎么解决的呀就是无法定位新窗口的 输入输出呀. 请发代码 新窗口也是get后得到的,赋值给变量就好了。 suchocolate 发表于 2020-8-10 10:39
新窗口也是get后得到的,赋值给变量就好了。
发代码看看不就行了吗 陈尚涵 发表于 2020-8-10 10:45
发代码看看不就行了吗
当然可以 谢谢指点
#!/usr/bin/env python
# coding:utf-8
import requests
import time
import json
import pyautogui
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
class zhixing(object):
def dianji(leixing,zhi):
print(leixing,zhi)
# 定位到元素
#double_click = browser.find_element_by_id('xx')
if "id" in leixing:
driver.find_element_by_id(zhi).click()
print(leixing,zhi,"点击完毕")
elif "name" in leixing:
driver.find_element_by_name(zhi).click()
print(leixing,zhi,"点击完毕")
elif "xpath" in leixing:
driver.find_element_by_xpath(zhi).click()
print(leixing,zhi,"点击完毕")
elif "class" in leixing:
driver.find_element_by_class_name(zhi).click()
print(leixing,zhi,"点击完毕")
elif "link" in leixing:
driver.find_element_by_link_text(zhi).click()
print(leixing,zhi,"点击完毕")
elif "tag" in leixing:
driver.find_element_by_tag_name(zhi).click()
print(leixing,zhi,"点击完毕")
elif "css" in leixing:
driver.find_element_by_css_selector(zhi).click()
print(leixing,zhi,"点击完毕")
elif "wenzi" in leixing:
driver.find_element_by_link_text(zhi).click()
print(leixing,zhi,"点击完毕")
elif "jpg" in leixing:
jpgxy=pyautogui.locateCenterOnScreen(zhi)
pyautogui.doubleClick(x=jpgxy-1,y=jpgxy,button="left")
print("图片点击完毕")
else:
print('请检查,',leixing,zhi,'内容是否正确')
#执行反馈
return
def shuru(leixing,zhi,neirong):
print(leixing,zhi)
if "id" in leixing:
driver.find_element_by_id(zhi).send_keys(neirong)
print("id输入完毕")
elif "name" in leixing:
driver.find_element_by_name(zhi).send_keys(neirong)
print("name输入完毕")
elif 'xpath' in leixing:
driver.find_element_by_xpath(zhi).send_keys(neirong)
print("xpath输入完毕")
elif 'input' in leixing:
driver.find_element_by_tag_name(leixing).send_keys(zhi)
print("xpath输入完毕")
else:
print('请检查,',shuru,'内容是否正确')
#执行反馈
return
def qqqiyehao(www,username,password):
driver.get(www)#打开网址
try:
with open('anquan.txt', 'r', encoding='utf8') as f:
listCookies = json.loads(f.read())
print('%%%%%%%%%%%%%%%%%', listCookies)
for cookie in listCookies:
driver.add_cookie(cookie)
driver.get(www)
# 读取完cookie刷新页面
driver.refresh()
print("调用cookie成功,尝试登录,直至登录成功")
except:
driverok=(driver.page_source)#获取源码
print(driverok)
if u"公众平台帐号登录" in driverok:
zhixing.dianji("xpath",'//*[@id="header"]/div/div/div/div/a')
# 输入账号密码
zhixing.shuru("name","account",username)
zhixing.shuru("name","password",password)
# 模拟点击登录
zhixing.dianji("xpath",'//*[@id="header"]/div/div/div/div/form/div/a')
# 等待3秒
time.sleep(20)
#获取网站cookie
dictCookies = driver.get_cookies()
jsonCookies = json.dumps(dictCookies)
# print(jsonCookies)
with open('anquan.txt', 'w') as f:
f.write(jsonCookies)
print("保存cookie成功")
# 将cookies保存
else:
print('登录不成功')
link = zhixing.dianji("xpath","/html/body/div/div/div/div/div/div/div/div/div/div/div")
time.sleep(3)
#执行到这里出问题了 因为弹出了新窗口
all_hand = driver.window_handles
print(all_hand)
driver.switch_to_window(all_hand[-1])
#driver.quit()
return
#执行反馈
if __name__ == '__main__':
desired_capabilities = DesiredCapabilities.FIREFOX
desired_capabilities["pageLoadStrategy"] = "eager"#normal 正常 eager 等解析完成 none当html下载完成
driver = webdriver.Firefox(executable_path='geckodriver.exe')
www=("https://mp.weixin.qq.com/")
username=("*******")
password=("*******")
zhixing.qqqiyehao(www,username,password)
陈尚涵 发表于 2020-8-10 10:34
请发代码
谢谢指教我的代码是这样的
#!/usr/bin/env python
# coding:utf-8
import requests
import time
import json
import pyautogui
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
class zhixing(object):
def dianji(leixing,zhi):
print(leixing,zhi)
# 定位到元素
#double_click = browser.find_element_by_id('xx')
if "id" in leixing:
driver.find_element_by_id(zhi).click()
print(leixing,zhi,"点击完毕")
elif "name" in leixing:
driver.find_element_by_name(zhi).click()
print(leixing,zhi,"点击完毕")
elif "xpath" in leixing:
driver.find_element_by_xpath(zhi).click()
print(leixing,zhi,"点击完毕")
elif "class" in leixing:
driver.find_element_by_class_name(zhi).click()
print(leixing,zhi,"点击完毕")
elif "link" in leixing:
driver.find_element_by_link_text(zhi).click()
print(leixing,zhi,"点击完毕")
elif "tag" in leixing:
driver.find_element_by_tag_name(zhi).click()
print(leixing,zhi,"点击完毕")
elif "css" in leixing:
driver.find_element_by_css_selector(zhi).click()
print(leixing,zhi,"点击完毕")
elif "wenzi" in leixing:
driver.find_element_by_link_text(zhi).click()
print(leixing,zhi,"点击完毕")
elif "jpg" in leixing:
jpgxy=pyautogui.locateCenterOnScreen(zhi)
pyautogui.doubleClick(x=jpgxy-1,y=jpgxy,button="left")
print("图片点击完毕")
else:
print('请检查,',leixing,zhi,'内容是否正确')
#执行反馈
return
def shuru(leixing,zhi,neirong):
print(leixing,zhi)
if "id" in leixing:
driver.find_element_by_id(zhi).send_keys(neirong)
print("id输入完毕")
elif "name" in leixing:
driver.find_element_by_name(zhi).send_keys(neirong)
print("name输入完毕")
elif 'xpath' in leixing:
driver.find_element_by_xpath(zhi).send_keys(neirong)
print("xpath输入完毕")
elif 'input' in leixing:
driver.find_element_by_tag_name(leixing).send_keys(zhi)
print("xpath输入完毕")
else:
print('请检查,',shuru,'内容是否正确')
#执行反馈
return
def qqqiyehao(www,username,password):
driver.get(www)#打开网址
try:
with open('anquan.txt', 'r', encoding='utf8') as f:
listCookies = json.loads(f.read())
print('%%%%%%%%%%%%%%%%%', listCookies)
for cookie in listCookies:
driver.add_cookie(cookie)
driver.get(www)
# 读取完cookie刷新页面
driver.refresh()
print("调用cookie成功,尝试登录,直至登录成功")
except:
driverok=(driver.page_source)#获取源码
print(driverok)
if u"公众平台帐号登录" in driverok:
zhixing.dianji("xpath",'//*[@id="header"]/div/div/div/div/a')
# 输入账号密码
zhixing.shuru("name","account",username)
zhixing.shuru("name","password",password)
# 模拟点击登录
zhixing.dianji("xpath",'//*[@id="header"]/div/div/div/div/form/div/a')
# 等待3秒
time.sleep(20)
#获取网站cookie
dictCookies = driver.get_cookies()
jsonCookies = json.dumps(dictCookies)
# print(jsonCookies)
with open('anquan.txt', 'w') as f:
f.write(jsonCookies)
print("保存cookie成功")
# 将cookies保存
else:
print('登录不成功')
link = zhixing.dianji("xpath","/html/body/div/div/div/div/div/div/div/div/div/div/div")
time.sleep(3)
#执行到这里出问题了 因为弹出了新窗口
all_hand = driver.window_handles
print(all_hand)
driver.switch_to_window(all_hand[-1])
#driver.quit()
return
#执行反馈
if __name__ == '__main__':
desired_capabilities = DesiredCapabilities.FIREFOX
desired_capabilities["pageLoadStrategy"] = "eager"#normal 正常 eager 等解析完成 none当html下载完成
driver = webdriver.Firefox(executable_path='geckodriver.exe')
www=("https://mp.weixin.qq.com/")
username=("*******")
password=("*******")
zhixing.qqqiyehao(www,username,password)
suchocolate 发表于 2020-8-10 10:39
新窗口也是get后得到的,赋值给变量就好了。
就是不会嘛多谢指教哦 一、多窗口就是多个对象
# 第一个窗口访问百度
driver1 = webdriver.Firefox()
driver1.get('http://www.baidu.com')
# 第二个窗口访问Sohu
driver2 = webdriver.Firefox()
driver2.get('http://www.sohu.com')
二、多选项卡(共用一个窗口)
browser = webdriver.Firefox()
browser.get('https://www.baidu.com')
browser.execute_script('window.open()')
print(browser.window_handles)
browser.switch_to.window(browser.window_handles)
browser.get('https://www.taobao.com')
time.sleep(1)
browser.switch_to.window(browser.window_handles)
browser.get('https://python.org')
driver.execute_script('window.open()') # 开启一个选项卡
windows=driver.window_handles # 获得当前浏览器所有窗口
driver.switch_to.window(windows) # 切换到最左侧窗口
driver.switch_to.window(windows[-1]) # 切换到最新打开窗口(注:也就是最右侧窗口)
{:10_257:}
页:
[1]