尝试访问“选择输入Python Splinter”时,元素不可见

时间:2018-07-24 08:08:31

标签: python selenium splinter

我正在尝试使用Python Splinter在选择下拉列表中单击某些元素。

源代码(例如-原始选项中包含更多选项值)是:

<div class="row form-group">
     <div class="col s12 m12 l6">
        <label>
        Zeitzone der Website
        </label>
        <div class="select-wrapper">
           <span class="caret">▼</span><input type="text" class="select-dropdown" readonly="true" data-activates="select-options-0388a2bc-2b5c-c534-8870-fc41b27e78c4" value="Wählen Sie eine Stadt">
           <ul id="select-options-0388a2bc-2b5c-c534-8870-fc41b27e78c4" class="dropdown-content select-dropdown ">
              <li class=""><span>Wählen Sie eine Stadt</span></li>
              <li class="optgroup-option "><span>Berlin</span></li>
           </ul>
           <select name="timezone" id="timezone-0" class="initialized">
              <option value="No timezone">Wählen Sie eine Stadt</option>
              <optgroup label="Europe">
                 <option value="Europe/Berlin">Berlin</option>
              </optgroup>
           </select>
        </div>
     </div>
  </div>

,我想选择柏林。

我尝试使用browser.find_by_css / text等,但是该元素不可见,因此成为异常。

Traceback (most recent call last):
  File "matomo.py", line 598, in <module>
    browser.find_option_by_text('Berlin').first.click()
  File "/usr/lib/python3.6/site-packages/splinter/driver/webdriver/__init__.py", line 562, in click
    self._element.click()
  File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated
  (Session info: chrome=67.0.3396.99)
  (Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.14.56-1-MANJARO x86_64)

有什么想法,解决方法或提示吗? :)

1 个答案:

答案 0 :(得分:0)

您可以尝试使用“明确等待”来等待它,

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable(By.XPATH, "//span[contains(text(),'Berlin')]")).click()

按Javascript函数的点击事件:

driver.execute_script("arguments[0].click()", ElementToClick)