handle tinymce window with python, selenium and phantomjs

China☆狼群 提交于 2021-02-07 13:44:05

问题


I have the following code for logging in on a site and post something in a forum

driver = webdriver.PhantomJS()

Username = "username"
Password = "password"

driver.get(LoginPage)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "login")))

driver.find_element_by_name("usr").send_keys(Username)
driver.find_element_by_name("pas").send_keys(Password)

driver.find_element_by_id("login").click()

payload = "some text"

driver.get(ForumPage)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "submitbtn")))


driver.switch_to_frame("tinymcewindow_ifr")
driver.find_element_by_id("tinymce").clear()
driver.find_element_by_id("tinymce").send_keys(payload)
driver.switch_to_default_content()

driver.find_element_by_id("submitbtn").click()

driver.quit()

When I try and use another browser (like firefox) it works fine, but with phantom nothing is posted.

What I believe could be the problem is that phantom doesn't fill in the tinymce textarea as i want it to. is there any fix for this problem?


回答1:


Use the tinymce js API.

Replace

driver.switch_to_frame("tinymcewindow_ifr")
driver.find_element_by_id("tinymce").clear()
driver.find_element_by_id("tinymce").send_keys(payload)
driver.switch_to_default_content()

with

driver.execute_script("tinyMCE.activeEditor.setContent('%s')" % payload)


来源:https://stackoverflow.com/questions/21203210/handle-tinymce-window-with-python-selenium-and-phantomjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!