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网页中的链接点击
请格式化回答 –