问题
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