WebDriver application example (java) - click to select the option containing a certain keyword in the floating box generated by Ajax

        Some of the tested pages contain the partial refresh mechanism of Ajax, and will generate a floating box that displays multiple pieces of data. You need to click to select an option that contains a keyword in the floating box.

        Tested web page: http://www.sogou.com

        Example code:

package cn.om.webdriverapi;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import java.util.List;
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.AfterMethod;

public class TestAjaxDivOption {

	WebDriver driver;
	String url;

	@Test
	public void testAjaxDivOption () {
		driver.get(url);
		WebElement input=driver.findElement(By.id("query"));
		//Click the search box to make the floating box appear
		input.click();
		// Store all options in the floating box in suggestionOptions
		List<WebElement> suggetionOptions=driver.findElements(By.xpath("//div[@id='vl']/div/ul/li"));
		//for loop to find the desired object
		for (WebElement element: suggetionOptions) {
			//Compare the content of each item with the target content, find the desired element, click
			System.out.println(element.getText());
			if(element.getText().contains("UAV")){//The content compared here is filled in according to the actual suspended content
				System.out.println(element.getText());
				element.click();
				break;
			}
		}
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		
	}

	@BeforeMethod
	public void beforeMethod() {
		url = "http://www.sogou.com";
		System.setProperty("webdriver.firefox.bin", "E:/Mozilla Firefox/firefox.exe");
		driver = new FirefoxDriver();

	}

	@AfterMethod
	public void afterMethod() {
		driver.quit();
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325770993&siteId=291194637