import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class JQuerySelectorExample { public static void main(String[] args) { String html = "<html><body><h1>Hello, World!</h1><p>This is a paragraph.</p></body></html>"; Document doc = Jsoup.parse(html); Elements paragraphs = doc.select("p"); for (Element paragraph : paragraphs) { System.out.println(paragraph.text()); } } } import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class JQueryEventHandlingExample { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("http://www.example.com"); WebElement element = driver.findElement(By.cssSelector("button")); String script = "$('button').click(function() { alert('Button clicked!'); });"; ((JavascriptExecutor) driver).executeScript(script, element); Actions actions = new Actions(driver); actions.moveToElement(element).click().perform(); Thread.sleep(3000); driver.quit(); } } import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; public class JQueryDOMManipulationExample { public static void main(String[] args) { String html = "<html><body><h1>Hello, World!</h1><p>This is a paragraph.</p></body></html>"; Document doc = Jsoup.parse(html); Element heading = doc.select("h1").first(); heading.text("Hello, Java!"); Element link = new Element("a"); link.attr("href", "https://www.example.com"); link.text("Click me"); doc.body().appendChild(link); Element paragraph = doc.select("p").first(); paragraph.remove(); System.out.println(doc.html()); } } import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.jsoup.Connection; import org.jsoup.Connection.Response; public class JQueryAJAXRequestExample { public static void main(String[] args) throws IOException { String url = "https://www.example.com/data"; Connection conn = Jsoup.connect(url); conn.method(Connection.Method.GET); conn.header("Content-Type", "application/json"); conn.timeout(5000); Response response = conn.execute(); Document doc = response.parse(); Elements dataElements = doc.select(".data"); for (Element dataElement : dataElements) { System.out.println(dataElement.text()); } } }


上一篇:
下一篇:
切换中文