【问题标题】:How do I edit CodeMirror with Selenium in Python?如何在 Python 中使用 Selenium 编辑 CodeMirror?
【发布时间】:2018-02-23 20:51:18
【问题描述】:

每次我尝试在我的网页上将文本插入 CodeMirror 时,都会收到下面的错误消息。有谁知道如何使用 selenium 成功编辑代码镜像?

WebDriverException: Message: unknown error: Cannot read property 'setValue' of undefined

这是我的 Selenium-Python 代码

def click_component_script_editor(self):
   driver = self.driver
   line18Edit = self.driver.find_element(By.XPATH, "//html//div[@class='CodeMirror-line']//div[18]/pre[1]")      
   driver.execute_script("arguments[0].CodeMirror.setValue(arguments[1]);",
                   line18Edit,
                   "foo.bar")

【问题讨论】:

    标签: python selenium codemirror


    【解决方案1】:

    想出了这个问题的答案,我不得不使用 actionChains 而不是普通的旧 send_keys。

    codeMirror = self.driver.find_element(".CodeMirror")
    action_chains.click(codeMirror).perform()
    action_chains.send_keys("Hello World").perform()
    

    【讨论】:

    • 如何将send_keys发送到codeMirror中的部分代码行?
    • 每次都会导致“无法聚焦元素错误”。
    • 好吧,事实证明我使用 wait.until 来确保 codemirror 元素的下一行是可见性并重复单击元素和 send_keys。 ui.WebDriverWait(driver, timeout=30).(expected_conditions.visibility_of_element_located(By.css, 'CodeMirror'))
    • 这里的action_chains 是什么? from selenium.webdriver.common import action_chains 导致错误AttributeError: module 'selenium.webdriver.common.action_chains' has no attribute 'click'
    • @Dan 问题是您尝试使用driver.find_element[s] 而不是driver.find_element。所以一个 s 有所作为。如果你想找到单个元素,你应该这样尝试: 使用这种方式:driver.find_element_by_link_text(".CodeMirror") 或者你想找到整个列表,使用这种方式:for x in self.driver.find_elements_by_link_text(".CodeMirror"):\n link =webdriver.ActionChains(self.driver).move_to_element(x).click(x).perform()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 2019-06-11
    • 2015-04-07
    • 2012-01-12
    相关资源
    最近更新 更多