Getting error: "is not a valid XPath expression." when trying to reference a field within service now

Viewed 557

I am trying to use Selenium/Webdriver and Python to automatically fill a form and was successful with all fields but one. I have a feeling it's because it is inside of a div container and I don't know how to specify something inside the container. I am trying to change the value "Blank".

This is what inspect element shows for the form field (highlighted)

The error line is from here:

assignmentGroup = web.find_element_by_xpath('//*div[@class=“input-group ref-container”]//sys_display.sc_req_item.assignment_group')

I have also tried doing the full xpath (copied from chrome) like this:

assignmentGroup = web.find_element_by_xpath('/html/body/div[2]/form/span[1]/span/div[5]/div[1]/div[2]/div[7]/div[2]/div[2]/input')

and like this, with and without the last *@id part:

assignmentGroup = web.find_element_by_xpath('//div[@class="col-xs-10 col-sm-9 col-md-6 col-lg-5 form-field input_controls"]//div[@class="input-group ref-container"]//*[@id="sys_display.sc_req_item.assignment_group"]')

Which gets me this from CLI- Message: no such element: Unable to locate element:>xpath here<

Last, I tried getting whatever attribute was in this box, just to see if I can locate it.

assignmentGroupAttribute = web.find_element_by_css_selector("div.input-group.ref-container").get_attribute('sys_display.sc_req_item.assignment_group')

My 8 hours of experience in Python/HTML/CSS may not be enough for me to figure this out.. Any thoughts?

1 Answers

I think you are making it more complicated than it needs to be. The input element you have highlighted has an id tag, try grabbing it by that

assignmentGroup = web.find_element_by_id("sys_display.sc_req_item.assignment_group")

Rule of thumb is to always search for ID or Name first, those are usually the most reliable. If those aren't available you can go into Xpath or CSS Selector

Related