为什么这个元素不可见(Selenium + Python / Django 1.9)

时间:2016-09-17 02:40:43

标签: python django selenium webdriver

我正在使用webdriver在Django中填写表单。找到并填写第一个字段name。但第二个领域不知何故被发现。这是我正在使用的脚本......

name = browser.find_element_by_id("name")
value = browser.find_element_by_id("value")
submit = browser.find_element_by_id("offer-submit")
name.send_keys(address)
name.send_keys(Keys.TAB)
# I tried having the browser press tab to see if it becomes visible. no luck.
value.send_keys(random.randrange(1, 100, 2))

这是错误追溯:

Traceback (most recent call last):
  File "populate_map.py", line 71, in <module>
    value.send_keys(random.randrange(1, 100, 2))
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 320, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 461, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
    at fxdriver.preconditions.visible (file:///c:/users/owner/appdata/local/temp/tmprd4j_t/extensions/fxdriver@googlecode.com/components/command-processor.js:10092)
    at DelayedCommand.prototype.checkPreconditions_ (file:///c:/users/owner/appdata/local/temp/tmprd4j_t/extensions/fxdriver@googlecode.com/components/command-processor.js:12644)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/owner/appdata/local/temp/tmprd4j_t/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///c:/users/owner/appdata/local/temp/tmprd4j_t/extensions/fxdriver@googlecode.com/components/command-processor.js:625)

使用此表单创建字段:

class OfferForm(forms.ModelForm):        
    service = forms.BooleanField()

    class Meta:
        model = Offer
        fields = [
            "name", 
            "value",
            "description",
            "tags",
            "location",
            "code",
            "service",
       #     "duration"
            "icon",
        ]
        widgets = {
            'name': forms.TextInput(
                attrs={'id': 'name', 'class': 'data', 'style': 'font-family: VT323; font-size: 60%', 'required': True, 'placeholder': 'name'}
            ),
            'value': forms.TextInput(
                attrs={'id': 'value', 'class': 'data', 'style': 'font-family: VT323; font-size: 60%', 'required': True, 'placeholder': 'value'}
            ),

}

阅读this question,我发现应该有一个很好的理由,即该值不可见 - 即使用样式属性使其不可见。但是当我提起萤火虫时,我看不到任何迹象表明它是隐形的。

这是我的模板代码:(我应该提一下,先前在脚本中单击了#offer,它会激活显示 - 当webdriver正在寻找它时,它不是没有。)

      <script>
      $("#offer").click(function(){
    $("#find-offer").css("display", "none");
    $("#make-offer").css("display", "block");
    $("#popular-offers").css("display", "block")
    $(".welcome").css("display", "none");
});
</script>

      <div id="make-offer" style="display: none">

      <p>Make an offer</p>
      <form name="offer-form" action="" method="post" enctype="multipart/form-data">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" id="offer-submit" class="data" style="font-family: Fira Mono; font-size: 70%; padding: 10px; position: absolute"  value="Submit" />
      </form>

还有其他想法吗?

1 个答案:

答案 0 :(得分:1)

根据我的经验,当我在硒中遇到这样的问题时,ActionChains通常就是答案。在这种情况下值得一试:

from selenium.webdriver.common.action_chains import ActionChains

ActionChains(browser).move_to_element(value).click().send_keys(random.randrange(1, 100, 2)).perform()

这将首先移动到元素,单击以聚焦输入,然后发送键。如果该元素未被视为可见,则可能会引发相同的异常,但值得一试。