如何使用python selenium处理模态?

时间:2018-02-23 07:45:11

标签: python selenium xpath selenium-webdriver modal-dialog

<h1 class="modalHeader"> Device Properties of test1234 </h1>

<div class="mainPopContent">
    <ul id="parent" class="tree">
        <li id="child">
            List Index: <span>1</span>
        </li>
        <li id="child">
            Device Type: <span>test 6000</span>

            <li id="child" class="has-children"><i class="js-toggle-icon">+</i>
            Network Mode
            <ul id="parent" style="display: none;">
                <li id="child">
                     Auto
                </li>
                <li id="child">
                    Firmware: <span>1.2.2.43</span>
                </li>
            </ul>
        </li>
    </ul>
 </div>

摘要:当我点击“编辑”按钮时,弹出模式正在打开,从该模态我需要获取以下详细信息。

我需要使用python selenium从模态中获取Device TypeFirmware文本。

我怎样才能解决这个问题?

请帮帮我

Stackrace:

DevTools listening on ws://127.0.0.1:12345/devtools/browser/af75ff22-1e5f-4493-8f56-158910238bf5
Device Type: EM 6000
Traceback (most recent call last):
  File "asif.py", line 12, in <module>
    driver.find_element_by_xpath("//div[@class='mainPopContent']/ul[@class='tree' and @id='parent']//following-sibling::li[2]/li/i[@class='js-toggle-icon']").click()
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 385, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element
    'value': value})['value']
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='mainPopContent']/ul[@class='tree' and @id='parent']//following-sibling::
li[2]/li/i[@class='js-toggle-icon']"}
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64)

2 个答案:

答案 0 :(得分:0)

根据HTML从模式中提取文本设备类型,您可以使用以下代码行:

Device_Type_element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='mainPopContent']/ul[@class='tree' and @id='parent']//following-sibling::li[2]")))
Device_Type = Device_Type_element.text

要点击网络模式,您可以使用以下代码行:

Network_Mode = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='mainPopContent']/ul[@class='tree' and @id='parent']//following-sibling::li[2]//i[@class='js-toggle-icon']")))
Network_Mode.click()

最后,根据HTML从模式中提取文本固件,您必须首先删除style="display: none;",然后您可以使用以下代码行:

Firmware_webelement = driver.find_element_by_xpath("//div[@class='mainPopContent']/ul[@class='tree' and @id='parent']//following-sibling::li[2]//ul//following::li[2]")
driver.execute_script("arguments[0].removeAttribute('style')", Firmware_webelement)
Firmware = driver.find_element_by_xpath("//div[@class='mainPopContent']/ul[@class='tree' and @id='parent']//following-sibling::li[2]//ul//following::li[2]").text

答案 1 :(得分:0)

我喜欢@DebanjanB的等待,但这种方法比需要的方法更复杂。这应该让你到那里(尝试手术XPath,它们不那么脆)。

find_element(By.XPATH, "//i[@class='js-toggle-icon']/..").click()
device_type = find_element(By.XPATH, "//li[contains(text(), 'Device Type')]/span").text
firmware = find_element(By.XPATH, "//li[contains(text(), 'Firmware')]/span").text