Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

python - 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

question from:https://stackoverflow.com/questions/65878108/selenium-send-keys-leaves-field-empty-with-no-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...