Selenium cant get or interact with custom profile after cookies sent (and other common solutions)?

huangapple go评论62阅读模式
英文:

Selenium cant get or interact with custom profile after cookies sent (and other common solutions)?

问题

#profile
chrome_options.add_argument(r"user-data-dir=C:\Users\prodi\AppData\Local\Google\Chrome\User Data\Profile 2")
#Path to your chrome profile

service = ChromeService(executable_path=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')

driver = webdriver.Chrome(service=service, options=chrome_options)

driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

#get url
tempUrl = "https://www.indeed.com/jobs?q=Web+Dev+Intern&l=Remote&sc=0kf%3Aattr%28DSQF7%29explvl%28ENTRY_LEVEL%29%3B&rbl=Remote&jlid=aaa2b906602aa8f5&pp=gQAAAAABhyaEcDMAAAAB_d4VZAADAAABAAA&vjk=e8d531753a776d38"

driver.get(tempUrl)

pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))

你好,我正在尝试使用我的Google配置文件访问一个网址,但它不起作用。Chrome配置文件弹出,但不执行任何操作。过一段时间后,它只会给出一个无法连接的错误。

以下是你提供的错误信息:

Traceback (most recent call last):
  File "c:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\Test Code\main.py", line 52, in <module>
    driver = webdriver.Chrome(service=service,options=chrome_options)
  File "C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 80, in __init__
    super().__init__(
  File "C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 101, in __init__
    self.service.start()
  File "C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\common\service.py", line 113, in start
    raise WebDriverException(f"Can not connect to the Service {self.path}")
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

我尝试了很多解决方案:

首先,请确保路径是正确的。它们看起来是正确的(https://i.stack.imgur.com/hO50z.jpg)。

其次,有一个解决方案建议使用一个Cookie,但没有起作用(https://stackoverflow.com/questions/71052074/selenium-get-not-working-with-custom-google-profile)。

其他解决方案也没有解决问题。你认为问题出在哪里?

英文:

#profile
chrome_options.add_argument(r&quot;user-data-dir=C:\Users\prodi\AppData\Local\Google\Chrome\User Data\Profile 2&quot;) 
#Path to your chrome profile

service = ChromeService(executable_path=r&#39;C:\Program Files (x86)\Google\Chrome\Application\chrome.exe&#39;)

driver = webdriver.Chrome(service=service,options=chrome_options)

driver.execute_script(&quot;Object.defineProperty(navigator, &#39;webdriver&#39;, {get: () =&gt; undefined})&quot;)

#get url
tempUrl = &quot;https://www.indeed.com/jobs?q=Web+Dev+Intern&amp;l=Remote&amp;sc=0kf%3Aattr%28DSQF7%29explvl%28ENTRY_LEVEL%29%3B&amp;rbl=Remote&amp;jlid=aaa2b906602aa8f5&amp;pp=gQAAAAABhyaEcDMAAAAB_d4VZAADAAABAAA&amp;vjk=e8d531753a776d38&quot;

driver.get(tempUrl)

pickle.dump(driver.get_cookies(), open(&quot;cookies.pkl&quot;, &quot;wb&quot;))


Hello I am trying to go to a url with my google profile. It isnt working. The chrome profile pops up but it dosent do anything. After a while it just gives me a cant connect error.

this:

Traceback (most recent call last):
  File &quot;c:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\Test Code\main.py&quot;, line 52, in &lt;module&gt;
    driver = webdriver.Chrome(service=service,options=chrome_options)
  File &quot;C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\chrome\webdriver.py&quot;, line 80, in __init__    
    super().__init__(
  File &quot;C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\chromium\webdriver.py&quot;, line 101, in __init__ 
    self.service.start()
  File &quot;C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\common\service.py&quot;, line 113, in start        
    raise WebDriverException(f&quot;Can not connect to the Service {self.path}&quot;)      
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

I tried many solutions

first is to make sure paths are right. They are looking https://i.stack.imgur.com/hO50z.jpg

second this:
one solution says to use a cookie. did nothing https://stackoverflow.com/questions/71052074/selenium-get-not-working-with-custom-google-profile

the others also do noting what is wrong?

答案1

得分: 1

The issue seems to be stemming from this line:

service = ChromeService(executable_path=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')
driver = webdriver.Chrome(service=service,options=chrome_options)

因为您正在使用 Chrome 浏览器可执行文件创建 ChromeService 实例,尽管您应该使用 Chromedriver 可执行文件。请参阅相关的 文档。 <br>

A fix for this that I would recommend you to do, since it's a pain to always manually upgrade your Chromedriver version whenever your Chrome Browser updates would be to use the webdriver-manager package. The code would then look like this:

from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
英文:

The issue seems to be stemming from this line:

service = ChromeService(executable_path=r&#39;C:\Program Files (x86)\Google\Chrome\Application\chrome.exe&#39;)
driver = webdriver.Chrome(service=service,options=chrome_options)

Because you are creating a ChromeService instance with the Chrome browser executable, even though you should be using the Chromedriver executable. See relevant docs. <br>
A fix for this that I would recommend you to do, since it's a pain to always manually upgrade your Chromedriver version whenever your Chrome Browser updates would be to use the webdriver-manager package.
The code would then look like this:

from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)

huangapple
  • 本文由 发表于 2023年4月4日 16:59:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定