简体   繁体   English

如何使用Selenium WebDriver移至网页中的其他选项卡?

[英]How to move to different tabs in a webpage using selenium webdriver?

I have a scenario where I need to click a tab and enter some text and come back to first tab. 我有一种情况,我需要单击一个选项卡并输入一些文本,然后返回第一个选项卡。

Assuming there are three tabs on page, How do i move to tab2 or tab3 and come back to tab1. 假设页面上有三个选项卡,如何移动到tab2或tab3并返回到tab1。

NOTE: I am just talking about tabs, Not windows. 注意:我只是在谈论选项卡,而不是Windows。 I tried all sorts findElement (webdriver's) methods, but no luck. 我尝试了各种findElement(webdriver)方法,但是没有运气。 By default i will be on ABSENCE (ie when page first loads). 默认情况下,我将处于“缺席”状态(即页面首次加载时)。 I have to move to EXCEPTIONS tab 我必须移至“例外”标签

I am using java as a code language. 我正在使用Java作为代码语言。

please see below code (TWO HTML TABS: 1) Absence 2) Exceptions). 请参见下面的代码(两个HTML标签:1)缺席2)异常)。 You can see tab name in span tab's. 您可以在跨度标签中看到标签名称。

<td nowrap="nowrap" align="center">
    <a accesskey="A" title="Absence" href="javascript:submitAction_RBET (document.win0,'TAB_BUTTON_ID','TIMESHEET#D');" name="TIMESHEET#D" tabindex="784">
       <span>
          <label class="PTUNDERLINE">A</label> bsence
       </span>
    </a>
</td>
<td nowrap="nowrap" align="center">
    <a accesskey="E" title="Exceptions" href="javascript:submitAction_RBET(document.win0,'TAB_BUTTON_ID','TIMESHEET#E');" name="TIMESHEET#E" tabindex="784">
       <span>
          <label class="PTUNDERLINE">E</label> xceptions
       </span>
    </a>
</td>   

I used below solution to switch over between the tabs. 我使用以下解决方案在选项卡之间切换。

     new Actions(driver)
    .sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL)
    .sendKeys(driver.findElement(By.tagName("html")), Keys.NUMPAD2)
    .build().perform();

In above Keys.NUMPAD2 refers that you are gonna move to the second tab in the session. 在上面的Keys.NUMPAD2 ,您将移至会话中的第二个选项卡。
You can move to Third, Fourth, etc... by giving NUMPAD3, NUMPAD4, etc... respectively. 您可以分别给NUMPAD3,NUMPAD4等来移动到Third,Fourth等。

I hope this will help you. 我希望这能帮到您。

I have a pretty hacky solution. 我有一个不错的解决方案。 I was in a similar situation and got around this problem by opening all of the tabs in a new window, by shift-clicking (This was on a Mac so the shortcut might be different on different platforms). 我处于类似情况下,通过按住Shift键并在新窗口中打开所有选项卡来解决此问题(这是在Mac上,因此快捷方式在不同平台上可能有所不同)。 It looks something like this: 看起来像这样:

Actions builder = new Actions(driver); 
Action holdShift = builder.keyDown(Keys.SHIFT).build();
holdShift.perform();
webElement.click();

new Actions(driver).keyUp(Keys.SHIFT).build().perform();
//do something with window handles/names here (can switch between these).

Can you put an id on the a tag? 您可以在标签上放置一个ID吗?

<a id="tab1"></a>

then use 然后使用

WebElement tab1 = driver.findElement(By.id("tab1"));
tab1.click();

And if you can't then you have other options: 如果不能,那么您还有其他选择:

http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/By.html http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/By.html

XPath would work but I can't say I like writing xpath expressions so I would try css selector. XPath可以工作,但是我不能说我喜欢编写xpath表达式,所以我会尝试使用CSS选择器。 See http://www.w3schools.com/css/css_attribute_selectors.asp Something like this (although this is quite brittle) 参见http://www.w3schools.com/css/css_attribute_selectors.asp这样的内容(尽管这很脆弱)

By.cssSelector("[title=Absence]");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM