简体   繁体   中英

How to click a random link in a web element

I am currently training in Selenium. The objective of the task is to:

  1. Navigate to a random Wikipedia page
  2. Pick a random category from the page's list of categories
  3. Select a random article from the list of pages in the selected category
  4. Output the compared differences between the categories of the original random page and the second one.

I have gotten all the way to the category page, but for some reason the code wont click on a random article within that page.

Here is my current code for the category page

        //Navigating to the category list at the bottom of the page
        WebElement Category = driver.findElement(By.id("mw-normal-catlinks"));
        Category.click();
        //finds a random link in the category list
        Random r = new java.util.Random();
        List<WebElement> links = driver.findElements(By.xpath("//div[@class = 'mw-category']"));    
        //picks a random link
        WebElement randomElement = links.get(r.nextInt(links.size()));
        randomElement.click();

        WebElement newCategory = driver.findElement(By.xpath("//div[@class= 'mw-category-generated']//div[@class= 'mw-category']"));
        newCategory.click();
        Random n = new java.util.Random();
        List<WebElement> ArticleLinks = driver.findElements(By.xpath("//div[@class= 'mw-category-generated']//div[@class= 'mw-category']"));
        WebElement randomElement2 = articleLinks.get(n.nextInt(articleLinks.size()));
        randomElement2.click();

The code seems good. I'm not sure why it's not working. Probably you are locating the random elements by randomElement and randomElement2 but you are clicking the elements. Please add below lines of code then it will work. randomElement.click() randomElement2.click()

A couple problems I see here:

List<WebElement> links = driver.findElements(By.tagName("mw-pages"));

Are you sure that the actual links are by tag mw-pages ? If you're not sure, take a look at the HTML. Does it show:

<mw-pages></mw-pages>

If not, then you're probably looking for a class or an ID. In which case, you need to use those selector strategies.

The other problem:

By.tagName("//div[@class= 'mw-category-generated']//div[@class= 'mw-category']")

The string you specified is not a tag name. Again, as mentioned above, By.tagName is looking for <tag-name> . This selector strategy is clearly an XPath, so use

By.xpath("//div[@class= 'mw-category-generated']//div[@class= 'mw-category']")

Additionally , drop the habit of PascalCasing your objects. Java is camelCased, not PascalCased. It's not relevant to your issue, but I think it's worth advising.

WebElement category; // not WebElement Category;
List<WebElement> articleLinks; // not List<WebElement> ArticleLinks;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM