sap
sap

Reputation: 13

How to construct a xpath or css locator to identify the element using Selenium and Java

Here is my Html:

<div class="x-form-item " tabindex="-1" id="ext-gen118">
    <label for="ext-comp-1060" style="width:40px;" class="x-form-item-label" id="ext-gen119">Name:</label>
    <div class="x-form-element" id="x-form-el-ext-comp-1060" style="padding-left:45px">
        <div class="x-form-field-wrap x-form-field-trigger-wrap x-trigger-wrap-focus" id="ext-gen120" style="width: 168px;">
            <input type="text" size="16" autocomplete="off" id="ext-comp-1060" name="ext-comp-1060" class="x-form-text x-form-field x-form-focus" style="width: 143px;" title="">
            <span class="x-form-twin-triggers" id="ext-gen121">
                <img src="/mco/extjs/resources/images/default/s.gif" alt="" class="x-form-trigger x-form-clear-trigger" id="ext-gen122" style="display: none;">
                <img src="/mco/extjs/resources/images/default/s.gif" alt="" class="x-form-trigger x-form-search-trigger" id="ext-gen123">
            </span>
        </div>
    </div>
    <div class="x-form-clear-left"></div>
</div>

I have tried below id "ext-comp-1060 is dynamic so that I cannot use.

xpath = "//div[@class='x-form-field-wrap x-form-field-trigger-wrap x-trigger-wrap-focus']//input[@class='input.x-form-text.x-form-field.x-form-focus']"

css = "input.x-form-text.x-form-field.x-form-focus"

In My application same input text field I have to use two times one after other first time it's working I have tried like this:

Wait wait = new FluentWait<WebDriver>(driver)
                .withTimeout(10, TimeUnit.SECONDS)
               .pollingEvery(3, TimeUnit.SECONDS);
       wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[contains(text(),'Name:')]")));
       ((JavascriptExecutor) driver).executeScript("arguments[0].click();", labelName);

searchName =@FindBy(css = "input.x-form-text.x-form-field.x-form-focus")
private WebElement searchByName;

searchByName.sendKeys(name);

But At same time second time it's not working

Upvotes: 0

Views: 315

Answers (6)

Evgeniy Chiruk
Evgeniy Chiruk

Reputation: 358

I updated your xpath using contains, try this:

xpath = "//div[contains(@class, 'x-form-field-wrap']/input[contains(@class, 'x-form-text']"

Upvotes: 0

Muzzamil
Muzzamil

Reputation: 2881

Your webelement is dynamic so you can look for partial matching for element's attribute like Id , class and Name

Wait for element

    By byCss=By.cssSelector("input[id^='ext-comp-']");
    By byXpath= By.xpath("//label[.='Name:']//input");

    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.elementToBeClickable(byCss));

    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.elementToBeClickable(byXpath));

Find element and Click:

    WebElement element = driver.findElement(byCss); 


    WebElement element = driver.findElement(byXpath);

    element.click(); 

Upvotes: 2

undetected Selenium
undetected Selenium

Reputation: 193358

To click() within the desired element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label.x-form-item-label[id^='ext-gen'][for^='ext-comp-']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@class='x-form-item-label' and starts-with(@id, 'ext-gen')][starts-with(@for, 'ext-comp-') and text()='Name:']"))).click();
    

Upvotes: 1

Zohair
Zohair

Reputation: 268

You can try below given xpath.

//div[@class='x-form-element']//child::input[@type='text' AND size='16']

I am accessing the input field using type and size attribute of the web element through a parent level element.

Hope this will solve your problem.

Upvotes: 0

KunduK
KunduK

Reputation: 33384

Use the following XPATH which will identify the input element with reference to label tag Name:

//label[text()='Name:']/following-sibling::div[1]//input

To handle dynamic element Induce WebDriverWait and wait for elementToBeClickable()

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[text()='Name:']/following-sibling::div[1]//input"))).sendKeys("Hello");

Upvotes: 0

Yalcin Batur
Yalcin Batur

Reputation: 59

Does only the numbers change or the whole word? If only the numbers change you can use this as Css locator:

input[id^='ext-comp']

Upvotes: 0

Related Questions