Tips and tricks for using WebDriver

本文详细介绍了使用WebDriver进行拖放操作的方法、如何通过FirefoxDriver更改用户代理以及如何在现有Firefox配置中进行调整。此外,还提供了在Firefox上启用特定功能的指南,并说明了如何设置语言偏好、查找配置键以及查找Firefox配置文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TipsAndTricks
Tips and tricks for using WebDriver
WebDriver
Updated Feb 2, 2012by dawagner@gmail.com

Tips and Tricks

Using Drag and Drop

It may not be immediately obvious, but if you're using a browser that supports it , you can use Action classes and then it's easy to do drag and drop:

 Actions builder = new Actions(driver);

   Action dragAndDrop = builder.clickAndHold(someElement)
       .moveToElement(otherElement)
       .release(otherElement)
       .build();

   dragAndDrop.perform();
   

Currently, only theFirefoxDriversupports this, but you should also expect support for theInternetExplorerDrivertoo.

Changing the user agent

This is easy with theFirefoxDriver:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override", "some UA string");
WebDriver driver = new FirefoxDriver(profile);

Tweaking an existing Firefox profile

Suppose that you wanted to modify the user agent string (as above), but you've got a tricked out Firefox profile that contains dozens of useful extensions. There are two ways to obtain this profile. Assuming that the profile has been created using Firefox's profile manager ("firefox -ProfileManager"):

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("WebDriver");
profile.setPreferences("foo.bar", 23);
WebDriver driver = new FirefoxDriver(profile);

Alternatively, if the profile isn't already registered with Firefox:

File profileDir = new File("path/to/top/level/of/profile");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.setPreferences(extraPrefs);
WebDriver driver = new FirefoxDriver(profile);

Enabling features that are disabled by default in Firefox

Native events is such a feature: It is disabled by default for Firefox on Linux as it may cause tests which open many windows in parallel to be unreliable. However, native events work quite well otherwise and are essential for some of the new actions of the Advanced User Interaction. To enable them:

FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);

How to set language in profile

profile.setPreference( "intl.accept_languages", "no,en-us,en" ); 

How to find profile keys

Look inprefs.jsof your Firefox profile.

How to find Firefox profile

Goto (url) about:support or FireFox Help menu / Troubleshooting information

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值