Skip to main content
 首页 » 编程设计

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

2025年04月02日22JeffreyZhao

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

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 ,我看到应该有一个很好的理由让该值不可见 - 即它正在通过 style 属性变得不可见。但是当我调出 Firebug 时,我看不到任何迹象表明它是不可见的。

这是我的模板代码:(我应该提到在脚本的前面点击了#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> 

还有其他想法吗?

请您参考如下方法:

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

from selenium.webdriver.common.action_chains import ActionChains 
 
ActionChains(browser).move_to_element(value).click().send_keys(random.randrange(1, 100, 2)).perform() 

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