select text from span in selenium java enviroment , when its not a drop box

Issue

so I want to select the text of span in selenium java, as it is not a normal drop-down box, I would have done it easily, I am using XPath I am able to open that drop-down box (where we select different sizes of shirt) type but no able to select text from it, I am sharing website link and also sharing my code
https://www.ounass.ae/shop-emporio-armani-camouflage-print-t-shirt-in-mercerised-cotton-for-men-215380566_20.html

my Code,

package First_Test;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ISelect;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.Keys;

import io.github.bonigarcia.wdm.WebDriverManager;

    public class Practice_First {
        
        public static void main(String[] args) throws InterruptedException {
            
            //System.setProperty("webdriver.chrome.driver", "./Driver/chromedriver.exe");
    
            WebDriverManager.chromedriver().setup();
            
            
            WebDriver driver = new ChromeDriver();
            
            driver.get("https://www.ounass.ae/");
    
            driver.manage().window().maximize();
    
            Thread.sleep(3000);
    
                         String originalWindow= driver.getWindowHandle();
    
                     Thread.sleep(5000);
        
                       driver.findElement(By.id("wzrk-cancel")).click();      
                       
                 // driver.findElement(By.xpath("//button[@id='wzrk-cancel']")).click();
                  driver.findElement(By.xpath("//a[@href='/men']//div[@class='Gender-centralize']//span[@class='Gender-CTA'][normalize-space()='Shop Now']")).click();
    
                  Thread.sleep(2000);
                 //driver.findElement(By.cssSelector("a[href='shop-emporio-armani-camouflage-print-t-shirt-in-mercerised-cotton-for-men-215380566_20.html']")).click();
                  // driver.findElement(By.cssSelector("a[title=\"Camouflage Print T-shirt in Mercerised Cotton\"]")).click();
                  
                  driver.findElement(By.xpath("//span[normalize-space()='675 AED']")).click();
    
                  Thread.sleep(3000);
    
                 driver.findElement(By.xpath("//button[@title='Blue']")).click();
    
                 Thread.sleep(2000);
                 
               driver.findElement(By.xpath("//div[@class='Select-control']")).click();
    
               Thread.sleep(1000);
    
               //driver.findElement(By.xpath("//div[@class='Select Dropdown SizeSelection-options 
             
                Thread.sleep(1000); */      
            //driver.close();
                
        }   
    }

My html code :

<div class="Select Dropdown SizeSelection-options SizeSelection-fixedSize has-value Select--single" xpath="1">
    
    <input name="sizeCode" type="hidden" value="75">
    
    <div class="Select-control">
    
    <div class="Select-multi-value-wrapper" id="react-select-sizeCode--value">
    
    <div class="Select-value">
    <span class="Select-value-label" role="option" aria-selected="true" id="react-select-sizeCode--value-item">M</span>
    </div>
    
    <div aria-expanded="false" aria-owns="" aria-activedescendant="react-select-sizeCode--value" aria-disabled="false" class="Select-input" role="combobox" style="border:0;width:1px;display:inline-block" tabindex="0">
    </div>
    
    </div>

    <span class="Select-arrow-zone"><svg width="12" height="7" viewBox="0 0 20 10" class="svg">
    
    <path d="M0 0l10 10L20 0" stroke="#2d2d2d" class="svg-stroke">
    </path>
    
    
</svg>
    
    

</span>
    
    

</div>
    
   

 </div>

Solution

Try below xPath for clicking on dropdown Item.

driver.FindElement(By.Xpath("//*[text()='XS']").Click();

Answered By – Ketan Pardeshi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published