vini007
vini007

Reputation: 597

Issue with identifying object using its ID?

enter image description hereenter image description hereI am working on a Salesforce application and am trying to choose a value from a drop down for my test case but I keep getting a NoSuchElement exception. I tried to identify the object using its ID

public void enterStep1Details()
{
    WebElement element = driver.findElement(By.id("pageid:theform:Block:NewHireRequisitionId:hm:HiringId"));
    element.sendKeys("C");
}

Below is the HTML code

<select id="pageid:theform:Block:NewHireRequisitionId:BusinessSegment:selectedReqTypeId" name="pageid:theform:Block:NewHireRequisitionId:BusinessSegment:selectedReqTypeId" size="1" onchange="A4J.AJAX.Submit('pageid:theform',event,{'similarityGroupingId':'pageid:theform:Block:NewHireRequisitionId:BusinessSegment:j_id57','oncomplete':function(request,event,data){RefreshText();},'parameters':{'pageid:theform:Block:NewHireRequisitionId:BusinessSegment:j_id57':'pageid:theform:Block:NewHireRequisitionId:BusinessSegment:j_id57'} } )" style="width:200px">
   <option value=""></option>
   <option value="Clinical Informatics">Clinical Informatics</option>
   <option value="Corporate Services">Corporate Services</option>
   <option value="Early Development Services">Early Development Services</option>
   <option value="Executive Office">Executive Office</option>
   <option value="Late Phase Services">Late Phase Services</option>
   <option value="Product Registration">Product Registration</option>
   <option value="Strategic Solutions">Strategic Solutions</option>
   <option value="Therapeutic Expertise">Therapeutic Expertise</option>
</select>

Upvotes: 0

Views: 73

Answers (3)

vini007
vini007

Reputation: 597

If the element is in a frame you will need to use switchTo() to get into the frame to access the element. You should be able to find lots of reference material on SO for this and elsewhere. – JeffC 21 hours ago

Upvotes: 0

JeffC
JeffC

Reputation: 25644

From what it looks like, it's built from other parts so you will likely need to use the entire ID to be sure that it's unique on the page. You could experiment with AGill's answer but I would just go with the entire ID.

WebElement element = driver.findElement(By.id("pageid:theform:Block:NewHireRequisitionId:BusinessSegment:selectedReqTypeId"));

After talking to the OP some more, turns out the element is in an IFRAME. An example of how to handle this is below.

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

after done in the frame, make sure you switch back to the default content.

driver.switch_to.default_content()

There are other options for accessing IFRAMEs but this should get you started.

Upvotes: 1

AGill
AGill

Reputation: 788

You can write a css selector for this. Look for the static portion of the id e.g. look for prefix or suffix or any substring which is unique and static and use that to write the css selector.

For example if id ends with text "selectedReqTypeId" you can write

By.cssSelector("[id$=selectedReqTypeId]")

Also you can write for matching pattern or substring like:

By.cssSelector("[id*='pattern']")

or as per your given code, you can probably write

By.cssSelector("[id*='NewHireRequisitionId']") //just an example

Similarly for startsWith, you can write:

By.cssSelector("[id^=startingText]")

Upvotes: 0

Related Questions