[英]i want to run a program to perform click on google apps icon using Selenium WebDriver but on running the code, it directs to google product page
[英]Selenium click action directs to page other than by Selenium IDE
我試圖使用Selenium WebDriver(2.21.0)單擊一個Web元素。
當我嘗試通過Selenium IDE進行驅動時,它可以正常工作,但是當我使用Firefox驅動程序的Java實現嘗試相同的一組操作時,它會導致錯誤的頁面。
當代碼運行時,我手動滾動到所需的元素,它的工作原理。
我確保web元素可見並啟用
By by = By.xpath("(//a[contains(@href, 'javascript:void(0);')])[26]"); //**Edit:** this is how i
//am getting the locator
WebElement element = driver.findElement(by);
return (element.isEnabled() || element.isDisplayed()) ? element : null;
它返回一些元素但不是我期待的元素。
這看起來很奇怪,因為Selenium webdriver主要滾動到一個元素(如果在屏幕上不可見),並進行所需的交互。
提前致謝!
編輯:這是IDE的導出代碼(java / JUnit4 / webdriver)
package com.example.tests;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Bandar {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://e.weibo.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testBandar() throws Exception {
driver.get(baseUrl + "/nescafechina");
driver.findElement(By.xpath("(//a[contains(@href, 'javascript:void(0);')])[26]")).click();
driver.findElement(By.xpath("(//a[contains(@href, 'javascript:void(0);')])[12]")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
Ishank,
我所做的工作已經完成並創建了一個測試,顯示了您可以在測試中使用的不同類型的斷言。 它們看起來與你所看到的有些不同,我覺得你的主要問題是WebElement element = driver.findElement(by);
因為你沒有給它一個實際的元素。 (by);
部分正在尋找可以在頁面上找到的字符串。 可接受的字符串是; id ("gbfqb");
或xpath ("(//a[contains(@href, 'javascript:void(0);')])[26]");
甚至是名字("find-button");
。
/**
* Test the main Google page.
* @throws InterruptedException
*
*/
@Test
public void signUp() throws InterruptedException {
String testId = "TestStack01";
entered(testId);
webDriver.get("www.google.com");
webDriver.findElement(By.id("gbqfq")).clear();
webDriver.findElement(By.id("gbqfq")).sendKeys("Test");
assertEquals("", webDriver.findElement(By.id("gbqfb")).getText());
WebElement whatyourlookingfor = webDriver.findElement(By.id("gbqfb"));
assertTrue(selenium.isElementPresent("gbqfb"));
assertTrue(whatyourlookingfor.isEnabled());
assertTrue(whatyourlookingfor.isDisplayed());
assertFalse(whatyourlookingfor.isSelected());
webDriver.findElement(By.id("gbqfb")).click();
leaving(testId);
}
我希望這有助於獲得返回的元素。
柯蒂斯米勒
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.