11 Essential Selenium Interview Questions *

最优秀的Selenium开发人员和工程师可以回答的基本问题. 在我们社区的推动下,我们鼓励专家提交问题并提供反馈.

Hire a Top Selenium Developer Now
Toptal logo是顶级自由软件开发人员的专属网络吗, designers, finance experts, product managers, and project managers in the world. 顶级公司雇佣Toptal自由职业者来完成他们最重要的项目.

Interview Questions

1.

What functions/methods can you use to wait for a page to load, or some element on the page to show up?

View answer

To wait for a page to load, explicit wait can be used. Explicit waits stall until some specific condition is met. Although Thread.Sleep() 是一种显式等待的形式,线程在特定的时间内停止, 等待页面加载可能不是最可靠的方式.

For example, rather than using something like:

Thread.Sleep(30000);

当线程休眠30秒(30,000毫秒)时,可以使用以下方法:

WebDriverWait wait= new WebDriverWait(webDriver,30);
wait.until(ExpectedConditions.visibilityOf(h1));

其中Selenium等待,直到给定的元素在页面上可见, or throws TimeoutException after waiting for 30 seconds.

2.

What is Page Object Model and Page Factory in Selenium?

View answer

Selenium中的页面对象模型是一种使用类来表示网页的设计模式. 类中的变量可以用来保存对网页上元素的引用, and methods can be used to implement behaviors. 这允许以一种优雅的方式实现既可读又易于维护和扩展的测试例程.

clickLoginButton();
setCredentials(user_name, user_password);
submitLoginForm();

页面工厂用于初始化每个WebElement变量,并使用配置的“定位器”将其引用到实际网页上的相应元素。. Annotations, such as @FindBy, can be used to define strategies for looking up elements, along with the necessary information for identifying them:

@FindBy(how=How.NAME, using="username")
private WebElement user_name;

More about Page Object Model and Page Factory can be found here.

3.

How do you deal with frame elements in Selenium on a page?

View answer

为了操作框架及其内容,您必须首先切换到它. 这类似于你必须切换到不同的页面才能与之交互:

driver.switchTo().frame(index);

… where index is the zero-based index of the frame. 切换帧将所有进一步的交互通过驱动程序指向选定的帧. The frame 方法也适用于名称、元素ID和对已定位元素的引用.

To switch back to the default frame, the defaultContent method can be used:

driver.switchTo().defaultContent();

Apply to Join Toptal's Development Network

and enjoy reliable, steady, remote Freelance Selenium Developer Jobs

Apply as a Freelancer
4.

CSS选择器和基于XPath的定位器之间有什么区别?

View answer
  • CSS selectors are often easier to read than XPath. 大多数前端开发人员可能已经很熟悉CSS选择器了.

  • 对CSS选择器的支持在各种现代web浏览器中是相当一致的, which is not the case with their XPath engines.

  • CSS selectors work faster than XPath.

For various reasons, such as the ones outlined above, Selenium“最佳实践”建议使用CSS选择器而不是XPath.

5.

如何编写定位符来标识div元素的直接子元素段落元素, or the descendent of a div element?

View answer

Immediate child:

An immediate child in XPath is defined indicated using “/”, while on CSS, it is indicated using “>”. For example, with XPath:

//div/p

… and with CSS:

div > p

Descendent:

查找任何div元素(i.e. 段落元素出现在以div元素为根的子树中), we can use “//” in XPath, and just a whitespace in CSS:

//div//p
div p
6.

简单解释一下下面这段Java代码的作用:

WebElement sample = driver.findElement(By.xpath("//*[contains(text(), 'data')]"));
View answer

It defines a variable sample of type WebElement, 并使用XPath搜索用一个包含文本值“data”的元素的引用来初始化它。.

7.

How can you send text input to a focused element?

View answer

这可以通过在聚焦的元素上模拟按键来实现. One way is to perform “actions” on the web driver object:

new Actions(webDriver).sendKeys(“some text”).perform();

另一种方法是先切换到活动元素,然后直接向它发送密钥:

webDriver.switchTo().activeElement().sendKeys(“some text”);
8.

Why do we use headless drivers? 当使用无头驱动程序时,如何直观地调查测试失败?

View answer

无头驱动通常用于持续集成(CI)设置. Headless drivers, such as PhantomJS, provide all standard web browser functionalities, but run in the command-line. 这些驱动程序基于命令行工具,不产生屏幕输出, making them ideal for completely automated setups.

To be able to visually investigate test failures, 开发人员需要实现捕获屏幕截图的机制, otherwise rely on command line output.

9.

How are absolute XPaths different from relative XPaths? 为什么在自动化测试中相对xpath通常优于绝对xpath?

View answer

就网页而言,绝对xpath从根元素开始:

html/head/body/table/tbody/tr/th

Relative XPaths, on the other hand, usually start with “//”:

//table/tbody/tr/th 

尽管这两个xpath可能都引用某个网页上的相同元素, 前者更有可能因对页面所做的任何更改而中断. For example, 将表移动到div元素内部将使绝对XPath无法定位该元素. 另一方面,相对XPath仍将继续工作.

10.

What is a hybrid framework in Selenium?

View answer

混合框架是关键字驱动框架和数据驱动框架的组合.

11.

What is StaleElementException ?

View answer

它是当被调用的元素由于任何原因不再附加到DOM时引发的异常.

There is more to interviewing than tricky technical questions, so these are intended merely as a guide. 并不是每一个值得雇佣的“A”候选人都能回答所有的问题, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains an art, a science — and a lot of work.

Why Toptal

Tired of interviewing candidates? Not sure what to ask to get you a top hire?

Let Toptal find the best people for you.

Hire a Top Selenium Developer Now

Our Exclusive Network of Selenium Developers

Looking to land a job as a Selenium Developer?

Let Toptal find the right job for you.

Apply as a Selenium Developer

Job Opportunities From Our Network

Submit an interview question

提交的问题和答案将被审查和编辑, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.

* All fields are required

Looking for Selenium Developers?

Looking for Selenium Developers? Check out Toptal’s Selenium developers.

Papuna Gagnidze

Freelance Selenium Developer
GeorgiaToptal Member Since November 21, 2019

Papuna是一名高技能的QA自动化和DevOps工程师,具有Linux系统管理和网络工程经验. 他拥有QA团队的领导经验和交付高质量端到端测试的技能. Papuna使用微服务和无服务器架构设计和实现了可扩展的系统.

Show More

Deniz Hamamcioglu

Freelance Selenium Developer
TurkeyToptal Member Since December 12, 2018

Deniz在软件测试自动化方面有超过9年的经验, framework, and test architecture design. 他领导和指导团队,并制定了有关测试的指导方针. Deniz is an expert on Selenium, Cypress, Playwright, WebdriverIO, Protractor, 以及许多其他测试自动化技术和编程语言. 他善于沟通,对细节有敏锐的洞察力. Deniz has been working remotely for the last couple of years, providing expert guidance across continents.

Show More

Jovana Rajkovic

Freelance Selenium Developer
SerbiaToptal Member Since March 22, 2018

For the past seven years, Jovana是一名QA工程师,与不同的QA团队合作,在多种类型的应用程序上进行复杂的功能测试(手动和自动化). 她对寻找漏洞充满热情,是一名勤奋的团队成员,热爱学习. Jovana在压力下茁壮成长,这可以从她成功处理截止日期和产品交付的历史中看出.

Show More

Toptal Connects the Top 3% of Freelance Talent All Over The World.

Join the Toptal community.

Learn more