0

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

2

6 Answers 6

2

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(); 
Sign up to request clarification or add additional context in comments.

Comments

1

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();
    

Comments

0

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']

2 Comments

Please add this as a comment of the question as it is definitely not an answer
edited my answer in somethign usefull anyway, thanks for adjusting @D.Lawrence
0

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");

2 Comments

Thanks all I have tried all solutions but nothing work for me
Actually I have two same input text field in my app so first one is working I have tried like this: css = "input.x-form-text.x-form-field.x-form-focus" with wait but at same time for second time its not working
0

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.

Comments

0

I updated your xpath using contains, try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.