Python函数强制打开一个新选项卡,而不是在当前页面中打开网站

时间:2019-04-18 15:25:13

标签: python selenium

当前,我正在尝试执行2个功能,登录到论坛并发表回复。登录是1个函数,发布回复是另一个函数,由于某种原因,当它是一个函数时,它会打开一个新选项卡,而不仅仅是在当前选项卡中打开URL。

我的代码就在这里:

    def __init__(self, login_url, time_for_bump):
        self.login_url = login_url
        self.time_for_bump = time_for_bump
        self.bump_phrase = "Bumpity bump"
        self.chrome = webdriver.Chrome(executable_path="C:/Users/Kaihan/Desktop/Programming/Web Driver/Chromedriver/chromedriver.exe")

    def login(self, username, password):
        self.chrome.get(self.login_url)
        time.sleep(1)
        self.chrome.find_element_by_css_selector(css_selector="#ctrl_pageLogin_login").send_keys(username)
        time.sleep(2)
        self.chrome.find_element_by_css_selector(css_selector="#ctrl_pageLogin_password").send_keys(password)
        time.sleep(1)
        self.chrome.find_element_by_css_selector(css_selector=".button").click()

    def bump_thread(self, thread):
        self.chrome.get(thread)
        time.sleep(2)
        self.chrome.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(2)
        self.chrome.find_element_by_xpath(xpath='//*[@id="QuickReply"]/div[1]/div/iframe').send_keys(self.bump_phrase)
        time.sleep(2)
        self.chrome.find_element_by_css_selector(css_selector="input.button:nth-child(3)").click()

0 个答案:

没有答案