如何在登录的slenium中执行类,而不是打开新的chrome实例? [英] How to execute class in logged in slenium instead of opening new chrome instance?
问题描述
from selenium import webdriver
from time import sleep
filename = "log.txt"
myfile = open(filename, 'w')
class Search(object):
def __init__(self):
self.driver = webdriver.Chrome('chromedriver.exe')
# "This will open a new chrome instance without being logged in to the site"
self.driver.get("Site2")
sleep(2)
self.driver.find_element_by_xpath("/html/body/div[1]/div[4]/div/div/div[3]/div/div/div[2]/div[2]/div[2]/div/div[4]/div[1]/div[2]/div[1]/a").click()
sleep(2)
Texto = self.driver.find_element_by_xpath("/html/body/div[1]/div[4]/div/div/div[4]/div/div/div[1]/div[3]/div/article[1]/div/div[2]/div/div/div/article/div[1]").text
print(Texto)
myfile.write(Texto)
myfile.close()
sleep(10)
import re
Id = re.compile('[0-9]{9}')
Id2 = re.compile('[0-9]{4}')
DobMonth = re.compile('[0-9]{2}')
DobYear = re.compile('[0-9]{2}')
if Id.match(Texto) and Id2.match(Texto) and DobMonth.match(Texto) and DobYear.match(Texto):
print ("Matches")
else:
print("Not match")
sleep (20)
Search()
class BasicBot(object):
def __init__(self, username, pw):
self.driver = webdriver.Chrome('chromedriver.exe')
self.driver.get("site1")
sleep(2)
self.driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div/div/div[1]/a[1]/span").click()
sleep(2)
self.driver.find_element_by_xpath("//input[@name=\"login\"]")\
.send_keys(username)
self.driver.find_element_by_xpath("//input[@name=\"password\"]")\
.send_keys(pw)
self.driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/form/div[1]/dl/dd/div/div[2]/button').click()
sleep(2)
Search()
BasicBot('username',"password")
因此该脚本运行BasicBot(usr,psswd)
登录到该站点,并且应该在登录时转到另一个站点,然后搜索X post是否符合给定的条件,如果符合,就是这样,否则应该刷新站点并再次检查.
So the script runs BasicBot(usr,psswd)
logs in to the site, and is supposed to go to a different site while logged in, then searches if X post matches the criteria given, if it does that's it if it does not, I should refresh the site and check again.
推荐答案
在Search类中,首先创建一个新的chromedriver实例:self.driver = webdriver.Chrome('chromedriver.exe')
,这就是为什么它会打开另一个具有新状态的窗口的原因.
In the Search class, you start with creating a new chromedriver instance: self.driver = webdriver.Chrome('chromedriver.exe')
, this is why it opens another window with a fresh state.
相反,将您的Search类修改为
Instead, modify your Search class to
- 采用现有的webdriver实例.
- 使用脚本
window.open
而不是get
打开新窗口:
- take an existing instance of webdriver.
- open a new window by using script
window.open
instead ofget
:
class Search:
def __init__(self, driver):
self.driver = driver
# Open a new window
self.driver.execute_script("window.open('https://site2')")
sleep(2) # visually check to make sure it opened
# Switch to new window
self.driver.switch_to.window(self.driver.window_handles[-1])
# After this you can start doing self.driver.find_element_by_xpath and the rest
在BasicBot类中,修改最后一行以传递驱动程序:
In the class BasicBot, modify the last line to pass the driver:
Search(self.driver)
最后,您可以使用刷新"来刷新您的站点2:
Lastly, you can use refresh to refresh your site2:
else:
print("Not match")
sleep(20)
self.driver.refresh()
希望,它会有所帮助.祝你好运!
Hope, it helps. Good luck!
这篇关于如何在登录的slenium中执行类,而不是打开新的chrome实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!