0

I am currently trying to send information to an input field in Chrome. I am working with variables when using the driver.find_element function.

What I have tried:

    ProjectID = '"' + str('driver.find_element(By.XPATH, "//input[@ID=' + "'" + '_obj__TIMESHEETITEMS_' + str(rowCounter) + '_-_obj__PROJECTID' + "']" + '").send_keys(' + "'" + str(nonProjects['Projects'][rowCounter-2]) + "'" + ')') + '"'

After the variables are applied, it looks like this when applying print(ProjectID):

"driver.find_element(By.XPATH, "//input[@ID='_obj__TIMESHEETITEMS_2_-_obj__PROJECTID']").send_keys('OMSH001')"

I also tried without the quotation marks in the front:

ProjectID = str('driver.find_element(By.XPATH, "//input[@ID=' + "'" + '_obj__TIMESHEETITEMS_' + str(rowCounter) + '_-_obj__PROJECTID' + "']" + '").send_keys(' + "'" + str(nonProjects['Projects'][rowCounter-2]) + "'" + ')')

Which looks like this when applying print(ProjectID):

driver.find_element(By.XPATH, "//input[@ID='_obj__TIMESHEETITEMS_2_-_obj__PROJECTID']").send_keys('OMSH001')

I am calling the variable with:

driver.execute_script(ProjectID)

The error I am getting without the quotation marks is that the driver is not defined. When applying the quotation marks, that error goes away, but then the program does not do anything.

Any help is greatly appreciated, as I have been stuck on this error for days.

1
  • Please include the full traceback error. Commented Jun 8, 2022 at 2:28

1 Answer 1

1

When looking at your examples, it seems as if you would try to use the selenium api in execute_script and this is obviously not possible.

ProjectID = '"' + str('driver.find_element(By.XPATH, "//input[@ID=' + "'" + '_obj__TIMESHEETITEMS_' + str(rowCounter) + '_-_obj__PROJECTID' + "']" + '").send_keys(' + "'" + str(nonProjects['Projects'][rowCounter-2]) + "'" + ')') + '"'
driver.execute_script(ProjectID)

The execute_script method executes the given JavaScript code directly in your browser and therefore whatever you want to do must be pure JavaScript.

It seems to me, as if in your example you do not need execute_script in the first place as you only directly call the selenium api.

I was not really able to fully unterstand your code, but would strongly suggest to simplify it it into something like this:

xpath = "//input[@ID='{}{}_-_obj__PROJECTID]".format(_obj__TIMESHEETITEMS_, rowCounter)
element = driver.find_element(By.XPATH, xpath)
element.send_keys(str(nonProjects["Projects"][rowCounter - 2]))
Sign up to request clarification or add additional context in comments.

1 Comment

I had to modify it a little bit to suit the required XPath structure, but this works amazingly well. Thank you!!

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.