【问题标题】:selenium webdriver not show dropdown list like a real browserselenium webdriver 不像真正的浏览器那样显示下拉列表
【发布时间】:2023-03-05 05:22:01
【问题描述】:

我使用 selenium webdriver 从 URL 获取一些内容,但我发现了以下错误: This picture show the dropdown list, I got the url content through the real browser

This picture not show the dropdown list, I got the url content through the selenium webdriver

我不知道为什么,请帮助我。非常感谢。 这是我的代码:

driver = webdriver.Chrome() driver.get("myurl")

【问题讨论】:

  • 原因是因为,selenium 驱动程序每次启动浏览器时都会使用新的配置文件(此配置文件未与您的任何其他帐户(如 gmail 或 fb)链接)但在实际浏览器中,它会与一些链接类型的帐户,基于此,我们看到的广告或类似的东西可能会有所不同。
  • @santhosh kumar 谢谢。有什么解决办法吗?

标签: selenium selenium-webdriver


【解决方案1】:

原因是,selenium 驱动程序每次使用新配置文件启动浏览器(此配置文件不与您的任何其他帐户,如 gmail 或 fb 链接)但在实际浏览器中,它会与某种帐户相关联并且基于在这一点上,我们看到的广告或类似的东西可能会有所不同。为了克服这个问题,我们可以使用我们真实浏览器使用的默认配置文件来启动我们的浏览器。下面的代码可能会给你一些想法。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)

要查找您的 chrome 配置文件数据的路径,您需要在地址栏中输入 chrome://version/。例如。我的显示为C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default,要在脚本中使用它,我必须排除\Default\,所以我们最终只得到C:\Users\pc\AppData\Local\Google\Chrome\User Data

另外,如果您想为 selenium 创建单独的配置文件:将路径替换为任何其他路径,如果它在启动时不存在,chrome 将为它创建新的配置文件和目录。

希望这对您有所帮助。谢谢。

【讨论】:

  • 太完美了!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 2013-12-25
  • 1970-01-01
相关资源
最近更新 更多