简体   繁体   English

如何在 Selenium Chromedriver 中设置时区?

[英]How do I set a timezone in Selenium Chromedriver?

I can't figure out how to set a timezone when using Chromedriver.我不知道如何在使用 Chromedriver 时设置时区。 Is there some ChromeOptions argument or something?是否有一些 ChromeOptions 参数之类的?

The issue is that when I go to some sites (for example, https://whoer.net ), it shows the system time that is equal to the time set on Windows.问题是,当我访问某些站点(例如https://whoer.net )时,它显示的系统时间等于 Windows 上设置的时间。 And I want to be able to change the Chromedriver's timezone somehow to perform timezone dependent testing.而且我希望能够以某种方式更改 Chromedriver 的时区以执行时区相关测试。

I tried to set some chrome options:我尝试设置一些 chrome 选项:

Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("args", Arrays.asList("--disable-system-timezone-automatic-detection", "--local-timezone"));
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

It doesn't work.它不起作用。

Tried to do some weird thing using Javascript:尝试使用 Javascript 做一些奇怪的事情:

((JavascriptExecutor) driver).executeScript("Date.prototype.getTime = function() { return 1 };");
   

It didn't help either.它也没有帮助。

EDIT:编辑:

Found this https://sqa.stackexchange.com/questions/8838/faking-system-time-date-with-selenium-webdriver发现这个https://sqa.stackexchange.com/questions/8838/faking-system-time-date-with-selenium-webdriver

Tried to execute javascript on page with the code copied from TimeShift.js like this:尝试使用从TimeShift.js复制的代码在页面上执行 javascript,如下所示:

((JavascriptExecutor) driver).executeScript("/*code from TimeShift.js here*/ TimeShift.setTimezoneOffset(-60);");

System time at https://whoer.net didn't change. https://whoer.net 上的系统时间没有改变。 What am I doing wrong?我做错了什么?

As far as I know you can only do this with TZ variable or with Decker Selenium.据我所知,您只能使用 TZ 变量或 Decker Selenium 执行此操作。

Were you able to find a viable solution to this however?但是,您是否能够找到可行的解决方案? I am trying to do the same exact thing and have found very little in the way of solutions.我正在尝试做同样的事情,但几乎没有找到解决方案。 I'm trying to do it with Python.我正在尝试用 Python 来做到这一点。 The Tz variable solution is limited to Firefox in windows and decker selenium is a nightmare to install on windows. Tz 变量解决方案仅限于 Windows 中的 Firefox,而在 Windows 上安装 decker selenium 是一场噩梦。

UPDATED PYTHON SOLUTION VIABLE (below)更新的 Python 解决方案可行(如下)

os.environ["DEBUSSY"] = "1" os.environ["DEBUSSY"] = "1"

SEE: How to set environment variables in Python参见: 如何在 Python 中设置环境变量

I am not sure if there is a Java equivalent but this Python equivalent worked for me in windows but only in Firefox.我不确定是否有 Java 等价物,但这个 Python 等价物在 Windows 中对我有用,但只在 Firefox 中。 If you find a Java equivalent then that's awesome but if this issue is important to you Python is the way to go!如果你找到一个 Java 等价物,那就太棒了,但如果这个问题对你很重要,那么 Python 就是你要走的路! :). :)。 I believe you are limited to three letters in windows but full customisation in Linux operating systems.我相信您在 Windows 中仅限于三个字母,但在 Linux 操作系统中是完全自定义的。 Eg UTC, GMT etc, etc,.例如 UTC、GMT 等,等等。 Hope this was helpful to you.希望这对你有帮助。 I spent ages looking for this but turns out I was overthinking it.我花了很长时间寻找这个,但结果是我想多了。 Good luck!!祝你好运!!

with new selenium 4, it can be done natively with CDP使用新的 selenium 4,它可以用CDP本地完成

    WebDriverManager.chromedriver().setup();
    WebDriver driver = new ChromeDriver();
    DevTools devTools = ((ChromeDriver) driver).getDevTools();
    devTools.createSession();
    devTools.send(Emulation.setTimezoneOverride("Antarctica/Casey"));

download the following maven dependency:下载以下 maven 依赖项:

      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.0.0-alpha-6</version>
    </dependency>

list of time zones can be found here时区列表可以在这里找到

You can do it by using Chrome DevTools Protocol, and here is the python code:您可以使用 Chrome DevTools 协议来实现,这里是 python 代码:

driver = webdriver.Chrome()
tz_params = {'timezoneId': 'America/New_York'}
driver.execute_cdp_cmd('Emulation.setTimezoneOverride', tz_params)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中将默认时区设置为爱尔兰/都柏林 - How do I set my default timezone to Ireland/dublin in javascript 如何在 Create React App 的 jest config 中设置时区? - How do I set the timezone in jest config in Create React App? 如何在 *Windows 10* 的 Jest 配置中设置时区? - How do I set a timezone in my Jest config in *Windows 10*? 如何指定时区,以便时刻可以给出该时区的正确日期? - How do I specify timezone such that moment will give proper date for that timezone? 如何在Selenium Webdriver JavaScript脚本中设置Cookie? - How do I set a cookie in the selenium webdriver javascript script? 如何使用Moment.js将澳大利亚/墨尔本时区设置为倒数计时器 - How do I set Australia/Melbourne timezone to a countdown timer using momentjs 如何在js中显示时区? - How do I display the timezone in js? 如何使用 javascript 获取时区? - How do I get the timezone using javascript? 如何使用 Capybara 和 ChromeDriver 模拟在输入字段中按 Enter 键? - How do I simulate hitting enter in an input field with Capybara and ChromeDriver? 如何从selenium webdriver启用chromedriver登录 - How to enable chromedriver logging in from the selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM