简体   繁体   中英

How to click on the element using Selenium through Java

I am trying to select value from drop down using adjacent sibling but does not work

Here is the Html:

<div id="dependsOnQuestionDiv"><div class="x-form-field-wrap x-form-field-trigger-wrap" id="ext-gen197" style="width: 17px;"><input type="text" size="24" autocomplete="off" id="dependsOnQuestion" name="dependsOnQuestion" class="x-form-text x-form-field x-trigger-noedit" readonly="" title="" style="width: 345px;"><img src="/mco/extjs/resources/images/default/s.gif" alt="" class="x-form-trigger x-form-arrow-trigger" id="ext-gen198"></div></div>

I have tried these ways:

(xpath =
 "//input[@id='dependsOnQuestion']/following-sibling::img[@id='ext-gen198']"

and

> css = #dependsOnQuestion ~img "

But it clicks on other element which has image tag

Here is another image tag HTML which is cliked when i am trying to click drop down

<td class="x-grid3-col x-grid3-cell x-grid3-td-edit  x-action-col-cell" style="width: 41px;" tabindex="0"><div class="x-grid3-cell-inner x-grid3-col-edit x-unselectable" unselectable="on"><img alt="" src="/mco/images/edit-button.png" class="x-action-col-icon x-action-col-0  "></div></td>

<div class="x-grid3-cell-inner x-grid3-col-edit x-unselectable" unselectable="on" xpath="1"><img alt="" src="/mco/images/edit-button.png" class="x-action-col-icon x-action-col-0 " style=""></div>

Can anyone help me?

Try Below XPATH options.

xpath = "//img[@id='ext-gen198']/preceding-sibling::input[@id='dependsOnQuestion']"

OR

 xpath = "//img[@id='ext-gen198']/preceding-sibling::input[1]"

To click() on the element you have 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("div#dependsOnQuestionDiv input.x-form-text.x-form-field.x-trigger-noedit#dependsOnQuestion[name='dependsOnQuestion']"))).click();
  • xpath :

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='dependsOnQuestionDiv']//input[@class='x-form-text x-form-field x-trigger-noedit' and @id='dependsOnQuestion'][@name='dependsOnQuestion']"))).click();

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