2
A
回答
5
捕捉和瀏覽所有的網頁鏈接
迭代器和先進的for循環可以做類似的工作;但是,循環內的頁面導航不一致可以使用數組概念來解決。
private static String[] links = null;
private static int linksCount = 0;
driver.get("www.xyz.com");
List<WebElement> linksize = driver.findElements(By.tagName("a"));
linksCount = linksize.size();
System.out.println("Total no of links Available: "+linksCount);
links= new String[linksCount];
System.out.println("List of links Available: ");
// print all the links from webpage
for(int i=0;i<linksCount;i++)
{
links[i] = linksize.get(i).getAttribute("href");
System.out.println(all_links_webpage.get(i).getAttribute("href"));
}
// navigate to each Link on the webpage
for(int i=0;i<linksCount;i++)
{
driver.navigate().to(links[i]);
Thread.sleep(3000);
}
1 |通過一個
driver.get("www.xyz.com");
WebElement element = driver.findElement(By.id(Value));
List<WebElement> elements = element.findElements(By.tagName("a"));
int sizeOfAllLinks = elements.size();
System.out.println(sizeOfAllLinks);
for(int i=0; i<sizeOfAllLinks ;i++)
{
System.out.println(elements.get(i).getAttribute("href"));
}
for (int index=0; index<sizeOfAllLinks; index++) {
getElementWithIndex(By.tagName("a"), index).click();
driver.navigate().back();
}
public WebElement getElementWithIndex(By by, int index) {
WebElement element = driver.findElement(By.id(Value));
List<WebElement> elements = element.findElements(By.tagName("a"));
return elements.get(index);
}
2 ID和一個導航|捕捉在特定幀中的所有鏈接|類|在一個陣列捕獲的所有鏈接[替代方法]
的Java
driver.get(baseUrl + "https://www.google.co.in");
List<WebElement> all_links_webpage = driver.findElements(By.tagName("a"));
System.out.println("Total no of links Available: " + all_links_webpage.size());
int k = all_links_webpage.size();
System.out.println("List of links Available: ");
for(int i=0;i<k;i++)
{
if(all_links_webpage.get(i).getAttribute("href").contains("google"))
{
String link = all_links_webpage.get(i).getAttribute("href");
System.out.println(link);
}
}
的Python
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.google.co.in/")
list_links = driver.find_elements_by_tag_name('a')
for i in list_links:
print i.get_attribute('href')
driver.quit()
0
存儲它們,然後點擊它們:
ArrayList<WebElement> input_type = (ArrayList<WebElement>)
driver.findElements(By.tagName("a"));
for (WebElement type : input_type)
{
type.click();
}
這會點擊一個個都與標籤鏈接,我希望你明白了。享受!
0
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.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class Find_all_Links {
private static String testUrl = "http://www.google.co.in/";
private static WebDriver driver = null;
public static void main(String[] args) {
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myProfile = profile.getProfile("AutomationQA");
driver = new FirefoxDriver(myProfile);
driver.get(testUrl);
List<WebElement> oLinksOnPage = driver.findElements(By.tagName("a"));
System.out.println(oLinksOnPage.size());
for(int i=0;i<oLinksOnPage.size();i++){
System.out.println(oLinksOnPage.get(i).getText());
}
}
}
1
public static void main(String[] args)
{
FirefoxDriver fd=new FirefoxDriver();
fd.get("http:www.facebook.com");
List<WebElement> links=fd.findElements(By.tagName("a"));
System.out.println("no of links:" +links.size());
for(int i=0;i<links.size();i++)
{
if(!(links.get(i).getText().isEmpty()))
{
links.get(i).click();
fd.navigate().back();
links=fd.findElements(By.tagName("a"));
}
}
}
這個程序點擊一個鏈接,導航回頁面,再次點擊第二個鏈接。
0
package selenium.tests;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestAllLinks {
public static void main(String[] args) {
String baseUrl = "http://www.qaautomated.com/";
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
String notWorkingUrlTitle = "Under Construction: QAAutomated";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl);
List<WebElement> linkElements = driver.findElements(By.tagName("a"));
String[] linkTexts = new String[linkElements.size()];
int i = 0;
//extract the link texts of each link element
for (WebElement elements : linkElements) {
linkTexts[i] = elements.getText();
i++;
}
//test each link
for (String t : linkTexts) {
driver.findElement(By.linkText(t)).click();
if (driver.getTitle().equals(notWorkingUrlTitle)) {
System.out.println("\"" + t + "\""
+ " is not working.");
} else {
System.out.println("\"" + t + "\""
+ " is working.");
}
driver.navigate().back();
}
driver.quit();
}
}
http://www.qaautomated.com/2016/10/selenium-test-to-check-links-in-web.html
相關問題
- 1. VBA點擊網頁鏈接
- 2. 點擊網頁中的鏈接
- 3. 無法點擊網頁中的鏈接
- 4. 如何點擊網頁C#中的鏈接
- 5. 點擊鏈接去不了網頁
- 6. 如何將網頁的所有鏈接轉換爲短網址?
- 7. 點擊包含字符串的頁面上的所有鏈接?
- 8. 如何獲取特定網站中的所有頁面鏈接?
- 9. 如何使用perl獲取網頁中的所有鏈接?
- 10. 如何查找和標記網頁中的所有鏈接?
- 11. 顯示網頁上的所有鏈接
- 12. jquery DomWindow網頁上的所有鏈接
- 13. 點擊所有鏈接並使用Selenium Ruby打開新標籤中的所有點擊鏈接
- 14. 點擊所有結果鏈接
- 15. 如何點擊沒有ID的鏈接
- 16. 如何通過mac-terminal打開網頁時點擊鏈接?
- 17. 如何點擊一個網頁鏈接在Visual Basic
- 18. WebView打開網頁,點擊鏈接顯示「網頁不可用」
- 19. HTML5:如何在點擊任何一個鏈接後禁用所有鏈接?
- 20. 從網頁提取所有鏈接
- 21. 鏈接網主頁的側邊欄的所有網頁
- 22. 點擊的iframe中的鏈接有在所有
- 23. Redis數據結構存儲所有鏈接的所有點擊
- 24. 如何點擊鏈接沒有文字?
- 25. 如何查找網站上的所有鏈接/頁面
- 26. 如何使網頁上的所有鏈接調用javascript函數?
- 27. 如何給我所有的網頁鏈接打開延遲?
- 28. 如何點擊ajax鏈接?
- 29. 無法點擊網頁中的鏈接使用硒webdriver
- 30. 識別iOS網頁中的鏈接點擊
請格式化回答 –