我有一个链接保留在同一页面上,但向下滚动屏幕
如果您访问网站 test.naimi.me 并单击此处:
该链接应该向下滚动一点,如何在 selenium webdriver 中测试此操作,我使用 testng 并用 java 编写。
编辑:@伸出援助之手
package erjan.test.naimi.me;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.Assert;
public class SpecialistLoads {
WebDriver firefox ;
@Test
public void main() {
//By.xpath("//a[@href=\"/astana/\"]/img") )) ;
WebElement all_specs = firefox.findElement(By.xpath("//a[@href=\"/astana/#specialists\"]")) ;
all_specs.click();
String all_specalists_url = firefox.getCurrentUrl();
Assert.assertEquals(all_specs, all_specialists_url );
}
@BeforeMethod
public void beforeMethod() {
firefox = new FirefoxDriver() ;
firefox.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch the website
firefox.get("http://test.naimi.me");
}
@AfterMethod
public void afterMethod() {
firefox.quit();
}
}
红色箭头指向的链接有html代码:
a data-toggle="menu" href="/astana/#specialists">Все специалисты</a>
最佳答案
如果页面加载时链接不可见,那么您可以使用 isdisplayed 方法,如下所示:
WebElement link = driver.findElement(By.xpath(yourlinkxpath"));
If(link.isDisplayed())
{
// Write code to click on link
}
else
{
// Write code to skip it
}
要查找链接元素,您可以使用 Id 、 xpath 、 class 任何内容。
关于java - 如何使用 webdriver、testng 编写测试来检查浏览器向下滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28601122/