简体   繁体   中英

java selenium xpath relative

I have to write a program that will search for light drones on Allegro. (using XPath relative)

Using Selenium Webdriver, open the Allegro portal in your browser, then switch the product category to Electronics and enter "Mavic mini" in the search field.

I'm using Java and IntelliJ for that, I got to the point when I'm going to the website, I click on the alert so it's closed and I put "Elektronika" into the category field. I can't write that part of code where it should type "mavic mini", here's how it all looks like;

    import org.openqa.selenium.Alert; 
    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.support.ui.Select; 
    import org.openqa.selenium.support.ui.WebDriverWait;


    public class AllegroTestingApp {
         public static void main(String[] args) {

         System.setProperty("webdriver.chrome.driver", "c:\\selenium-drivers\\chrome\\chromedriver.exe");
         WebDriver driver = new ChromeDriver();
         driver.get("https://www.allegro.pl");
 
         WebElement categoryCombo = driver.findElement(By.xpath("//div//div//select"));
         Select categorySelect = new Select(categoryCombo);
         categorySelect.selectByIndex(3);
         driver.manage().window().maximize();
         driver.findElement(By.xpath("/html/body/div[2]
        /div[8]/div/div[2]/div/div[2]/button[2]")).click();
        Alert simpleAlert = driver.switchTo().alert();
        simpleAlert.accept();    
        WebElement inputField = 
        driver.findElement(By.xpath("//INPUT[@type='search']/self::INPUT"));
        inputField.sendKeys("mavic mini");
        inputField.submit();
 } 

Please help!

I see the code given in question is working by just commenting below lines. Seems like no alert anymore in that page.

Alert simpleAlert = driver.switchTo().alert();
simpleAlert.accept(); 

A few feedback items:

  1. Your locator for categoryCombo is very broad/generic and should be more specific when possible. Always look for an ID, a name, or some other attribute that is unique on the page. In this case, it has a unique aria-label that we can use to make a unique locator, By.cssSelector("select[aria-label='Kategoria i opcje wyszukiwania']")

  2. Rather than select by index, you should choose to send the actual value you are looking for, eg "Elektronika". That makes it much more readable and if the categories change (which changes the indices), your code will still work. For this you can use Select.selectByVisibleText() .

  3. The next part of your code I don't understand. You are clicking a button and then accepting a JS alert? I'm not seeing that so I'm not sure what you are seeing there or if you were trying to accept/close the initial popup before setting the dropdown? If so, your code is out of order.

  4. Your locator for the search box was working but was not specific and had some irrelevant parts, eg the ".../self::INPUT" part is not necessary. As in my first point, you want to make your locator as specific as possible using ID, name, etc. In this case, the search box has a name so I used it, By.cssSelector("input[name='string']") .

  5. Most of the time, people that search for one item on a site want to eventually search for another item. In that case, it's best to package up your search related code into a method. I created SelectCategory() that takes in the name of a category and sets it. I also created Search() which takes in the category name and the search string and does the complete search. You can see them both in the code below.

  6. Because I moved some code into different methods, I moved the driver instance variable to the top of the class so that you don't have to pass it into each method.

Here's my working code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class AllegroTestingApp
{    
    static WebDriver driver;

    public static void main(String[] args)
    {
        System.setProperty("webdriver.chrome.driver", "c:\\selenium-drivers\\chrome\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();

        String url = "https://www.allegro.pl";
        driver.get(url);

        // wait for popup and close
        new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("img[alt='zamknij']"))).click();

        Search("Elektronika", "mavic mini");

        driver.quit();
    }

    public static void Search(String categoryName, String searchString)
    {
        SelectCategory(categoryName);
        WebElement searchBox = driver.findElement(By.cssSelector("input[name='string']"));
        searchBox.sendKeys(searchString);
        searchBox.submit();
    }

    public static void SelectCategory(String categoryName)
    {
        By categoryDropdownLocator = By.cssSelector("select[aria-label='Kategoria i opcje wyszukiwania']");
        new Select(driver.findElement(categoryDropdownLocator)).selectByVisibleText(categoryName);
    }
}

Try this code it will work:

driver.get("https://www.allegro.pl");
        WebElement categoryCombo = driver.findElement(By.xpath("//div//div//select"));
        Select categorySelect = new Select(categoryCombo);
        categorySelect.selectByIndex(3);
        driver.manage().window().maximize();
        driver.findElement(By.xpath("/html/body/div[2]/div[8]/div/div[2]/div/div[2]/button[2]")).click(); 
       WebElement inputField =driver.findElement(By.xpath("//input[@name='string']"));
       inputField.sendKeys("mavic mini");
       inputField.submit();

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