Synns
Synns

Reputation: 1

Selenium; send_keys leaves field empty with no error

total beginner here. I am automating a form used for ''non-conformity'' ticket, and I am filling all the text field sucessfully except one. The thing is, I do not receive any error message. The code go all the way to the end but leave one text field empty. That text box is the body of the ticket and is used to decribe the issue, so I can't work my way around not using it.

Here is the code I wrote initially:

print(desc_to_write) #Use while debuggin this to confirm the variable contain the string.

desc = web.find_element_by_xpath('/html/body')
desc.send_keys(desc_to_write)
#No error but still nothing in the browser text field. 

The xpath is what I get with Chrome's ''Copy Xpath'' tool.

I have also tried the following methods with no success (assiociated comment are the error received).

#1:

 desc = web.find_element_by_class_name('editor_body')
 desc.send_keys(desc_to_write)
#selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".editor_body"}

#2 :

    desc = web.find_element_by_css_selector('body.editor_body')
    desc.send_keys(desc_to_write)
    #NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"body.editor_body"}

#3 :

desc = web.find_element_by_xpath('/html/body')
desc.click()
time.sleep(5)
desc.send_keys(desc_to_write)
#No error but still nothing in the browser text field. 

#4 : I have also tried with a full Xpath with no success. I had to guess the full by myself as the ''Copy full Xpath'' tool would return the same path as above. I got most of that path using the Xpath of the ''iframe'' and adding the ''/html/body''. Note that I know nothing about HTML, so I might have done a mistake somewhere. It was an out-of-desparation attempt.

desc = web.find_element_by_xpath('//*[@id="NewRequestTab"]/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/table/tbody/tr[14]/td[2]/div/iframe/html/body')
desc.send_keys(desc_to_write)

The resulting error is :

#selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="NewRequestTab"]/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/table/tbody/tr[14]/td[2]/div/iframe/html/body"}  (Session info: chrome=88.0.4324.104)

I am also attaching a screen shot of the code as seen in the Chrome's developper tool. I could copy the actual code but I honestly know how HTML work and I do not know what would be relevant. I can provide more if needed. The highlighted part is the result of the inspector tool in Chrome's developper tool.

Thanks for the help!

HTML CODE

Upvotes: 0

Views: 683

Answers (2)

Synns
Synns

Reputation: 1

I did some research base on John answer and found the following answer to similar issue. I then came up with

web.switch_to.frame(web.find_element_by_xpath('//*[@id="NewRequestTab"]/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/table/tbody/tr[14]/td[2]/div/iframe'))
desc = web.find_element_by_class_name('editor_body')
desc.click()
desc.send_keys(desc_to_write)
web.switch_to.default_content()

And now everything is fine. Thanks!

Upvotes: 0

K.Mat
K.Mat

Reputation: 1620

You have to switch to the iframe, in order to use its contents with selenium. The best method is to first wait for it to appear, and then switch:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 300)
wait.until(EC.frame_to_be_available_and_switch_to_it(driver.find_element(By.CLASS_NAME, 'textarea')))

Upvotes: 1

Related Questions