Java 我有100个+;按钮,我必须单击每个按钮,并验证单击后打开的链接和页面
我在页面中有100多个按钮,我必须单击每个按钮并验证单击后打开的链接和页面 所以如何在不为所有100+按钮编写xpath的情况下单击,我只需要单击它们,每个按钮都会打开不同的页面Java 我有100个+;按钮,我必须单击每个按钮,并验证单击后打开的链接和页面,java,selenium,testing,Java,Selenium,Testing,我在页面中有100多个按钮,我必须单击每个按钮并验证单击后打开的链接和页面 所以如何在不为所有100+按钮编写xpath的情况下单击,我只需要单击它们,每个按钮都会打开不同的页面 请帮助我继续我的selenium测试在您单击第一个按钮后,将加载一个新的DOM,因此单击第二个(和第三个…)将以StaleElementReferenceException结束。您需要先获取所有href值,然后逐个尝试。刚刚在web bbc.com上试用了以下代码: package navi; import java.
请帮助我继续我的selenium测试在您单击第一个按钮后,将加载一个新的DOM,因此单击第二个(和第三个…)将以
StaleElementReferenceException
结束。您需要先获取所有href值,然后逐个尝试。刚刚在web bbc.com上试用了以下代码:
package navi;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Himanshu {
public static WebDriver driver;
@BeforeClass
public static void setUpClass() {
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
}
@Before public void setUp() {} @After public void tearDown() {}
@AfterClass public static void tearDownClass() {driver.quit();}
@Test
public void lots_of_buttons() throws InterruptedException {
driver.get("http://www.bbc.com/");
// wait to page is fully loaded
wait_sec(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"orb-header\"]/div[1]/div[1]/a/img")));
// get all the desired webelements
List<WebElement> header_links = driver.findElements(By.xpath("//a[contains(@href,'x')]"));
// create new arraylist
ArrayList<String> hrefs = new ArrayList<String>();
// collect all the href values
for (WebElement link: header_links) {
hrefs.add(link.getAttribute("href"));
}
// visit all the URLs
for (String url: hrefs) {
driver.get(url);
Thread.sleep(1000);
// do some stuff here
}
}
// modified wait method
public WebDriverWait wait_sec(WebDriver driver, int sec) {
return new WebDriverWait(driver, sec);
}
}
package-navi;
导入java.util.ArrayList;
导入java.util.List;
导入org.junit.After;
导入org.junit.AfterClass;
导入org.junit.Before;
导入org.junit.BeforeClass;
导入org.junit.Test;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.firefox.FirefoxOptions;
导入org.openqa.selenium.firefox.FirefoxProfile;
导入org.openqa.selenium.firefox.internal.ProfilesIni;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.WebDriverWait;
公务舱希曼殊{
公共静态WebDriver;
@课前
公共静态void setUpClass(){
FirefoxOptions=新的FirefoxOptions();
ProfilesIni allProfiles=新ProfilesIni();
FirefoxProfile selenium_profile=allProfiles.getProfile(“selenium_profile”);
选项。设置配置文件(硒_配置文件);
options.setBinary(“C:\\ProgramFiles(x86)\\Mozilla Firefox\\Firefox.exe”);
System.setProperty(“webdriver.gecko.driver”,“C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe”);
驱动程序=新的Firefox驱动程序(选项);
driver.manage().window().maximize();
}
@在public void setUp()之前{}@在public void拆卸()之后{}
@AfterClass公共静态void tearDownClass(){driver.quit();}
@试验
public void lots\u of_buttons()引发InterruptedException{
驱动程序。获取(“http://www.bbc.com/");
//等待页面完全加载
等待(驱动程序,5).until(ExpectedConditions.elementtobelickable(By.xpath(“/*[@id=\\”orb header\”]/div[1]/div[1]/a/img”);
//获取所有所需的WebElement
列表标题_links=driver.findElements(By.xpath(//a[contains(@href,'x')]);
//创建新的arraylist
ArrayList hrefs=新的ArrayList();
//收集所有href值
for(WebElement链接:标题链接){
添加(link.getAttribute(“href”);
}
//访问所有URL
for(字符串url:hrefs){
获取(url);
睡眠(1000);
//在这里做些事情
}
}
//修正等待法
公共WebDriverWait等待秒(WebDriver-driver,int-sec){
返回新的WebDriverWait(驱动程序,秒);
}
}
er,请使用循环?html片段如何共享?HTML内容从DOM复制HTML内容,并通过编辑当前问题将其粘贴到此处。