【问题标题】:Reopen same browser window using selenium python and Firefox使用 selenium python 和 Firefox 重新打开相同的浏览器窗口
【发布时间】:2023-04-06 01:09:02
【问题描述】:

嘿,我正在尝试制作一个自动程序来发送 Whatsapp 消息。 我目前正在使用 python、Firefox 和 selenium 来实现这一点。 问题是每次我调用driver.get(url) 时,它都会打开一个新的Firefox 浏览器实例,空白且没有上次运行的记忆。它让我每次运行时都要扫描条形码。

from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxProfile

cp_profile = webdriver.FirefoxProfile("/Users/Hodai/AppData/Roaming/Mozilla/Firefox/Profiles/v27qat5d.whatsapp_profile")
driver = webdriver.Firefox(executable_path="/Users/Hodai/Desktop/geckodriver",firefox_profile=cp_profile)
driver.get('http://web.whatsapp.com')

#Scan the code before proceeding further
input('Enter anything after scanning QR code')

我尝试过使用个人资料,但似乎没有任何影响。

cp_profile = webdriver.FirefoxProfile("/Users/Hodai/AppData/Roaming/Mozilla/Firefox/Profiles/v27qat5d.whatsapp_profile")
driver = webdriver.Firefox(executable_path="/Users/Hodai/Desktop/geckodriver",firefox_profile=cp_profile)

【问题讨论】:

    标签: python selenium firefox webdriver selenium-chromedriver


    【解决方案1】:

    最后我使用 chromedriver 来实现我的目标。 我尝试了带有泡菜的 cookie,但它有点棘手,因为它只记住了同一个域的 cookie。 所以我使用了 chrome 的用户数据。 现在它就像一个魅力。谢谢大家。

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("user-data-dir=C:/Users/Designer1/AppData/Local/Google/Chrome/User Data/Profile 1")
    driver = webdriver.Chrome(chrome_options=options,executable_path="C:\webdrivers\chromedriver.exe")
    

    【讨论】:

      【解决方案2】:

      我认为最简单的方法是在扫描 qrcode 后保存 cookie 并手动将它们推送到 Selenium。

      # Load page to be able to set cookies
      driver.get('http://web.whatsapp.com')
      
      # Set saved cookies
      cookies = {'name1': 'value1', 'name2', 'value2'}
      for name in cookies:
          driver.add_cookie({
              'name': name,
              'value': cookies[name],
          })
      
      # Load page using cookies
      driver.get('http://web.whatsapp.com')
      

      要获取您的 cookie,您可以使用控制台 (F12),网络选项卡,右键单击请求,复制 => 复制请求标头。

      【讨论】:

        【解决方案3】:

        不应该是这样的。它仅在使用新变量初始化或程序再次启动时打开新窗口。这是chrome的代码。无论您调用多少次driver.get(url),它都会在同一个浏览器窗口中打开网址

        from selenium import webdriver
        import selenium.webdriver.support.ui as ui
        from selenium.webdriver.common.keys import Keys
        from selenium.common.exceptions import NoSuchElementException
        from selenium.webdriver.common.by import By
        import time
        driver = webdriver.Chrome(executable_path=r"C:\new\chromedriver.exe")
        driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
        time.sleep(10)
        driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
        time.sleep(10)
        driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
        time.sleep(10)
        

        如果问题已解决或您正在尝试做其他事情,请告诉我。

        【讨论】:

          猜你喜欢
          • 2021-03-03
          • 2014-09-04
          • 2021-10-03
          • 1970-01-01
          • 2017-08-01
          • 2016-10-12
          • 2014-08-29
          • 2013-06-23
          相关资源
          最近更新 更多