元素目前不可见,因此可能无法与[Firefox]进行交互

时间:2015-01-08 09:27:55

标签: python-2.7 selenium-webdriver

我试图点击名为" Fast Path"的按钮。然后输入1067.但它会抛出错误,如元素当前不可见,因此可能无法与进行交互 如果有帮助,这里是Chrome的页面来源。

<span class="textUpperCase" style="position:relative;" id="pfx$txtFastPath-containerNode" onmouseout="JKB.widget.manager.getWidget('pfx$txtFastPath').onMouseOut(event)" onmouseover="JKB.widget.manager.getWidget('pfx$txtFastPath').onMouseOver(event)"><label id="pfx$txtFastPath-labelNode" for="pfx$txtFastPath">Fast Path :</label><input type="text" autocomplete="off" name="txtFastPath" class="textUpperCase focus focus" style="position:absolute;left:90px;top:0px;;text-align:left;margin:0px" size="12" id="pfx$txtFastPath" onblur="JKB.widget.manager.onHtmlBlur('pfx$txtFastPath',event)" onchange="JKB.widget.manager.getWidget('pfx$txtFastPath').onChange(event)" onfocus="JKB.widget.manager.onHtmlFocus('pfx$txtFastPath',event)" onkeydown="JKB.widget.manager.getWidget('pfx$txtFastPath').onKeyDown(event)" onkeyup="JKB.widget.manager.getWidget('pfx$txtFastPath').onKeyUp(event)" onkeypress="JKB.widget.manager.getWidget('pfx$txtFastPath').onKeyPress(event)" tabindex="0"></span>

编辑:更易读的版本(在语义上相同,但原始版本保留为安全):

<span class="textUpperCase" style="position:relative;" id="pfx$txtFastPath-containerNode"
      onmouseout="JKB.widget.manager.getWidget('pfx$txtFastPath').onMouseOut(event)"
      onmouseover="JKB.widget.manager.getWidget('pfx$txtFastPath').onMouseOver(event)">
 <label id="pfx$txtFastPath-labelNode" for="pfx$txtFastPath">Fast Path :</label>
 <input type="text" autocomplete="off" name="txtFastPath"
        class="textUpperCase focus focus"
        style="position:absolute;left:90px;top:0px;;text-align:left;margin:0px"
        size="12" id="pfx$txtFastPath"
        onblur="JKB.widget.manager.onHtmlBlur('pfx$txtFastPath',event)"
        onchange="JKB.widget.manager.getWidget('pfx$txtFastPath').onChange(event)"
        onfocus="JKB.widget.manager.onHtmlFocus('pfx$txtFastPath',event)"
        onkeydown="JKB.widget.manager.getWidget('pfx$txtFastPath').onKeyDown(event)"
        onkeyup="JKB.widget.manager.getWidget('pfx$txtFastPath').onKeyUp(event)"
        onkeypress="JKB.widget.manager.getWidget('pfx$txtFastPath').onKeyPress(event)"
        tabindex="0">
</span>

2 个答案:

答案 0 :(得分:1)

在呈现所提供的代码时,我没有看到要点击的按钮,因此我只能在一般意义上描述您可能需要做什么。

首先,您可能需要告诉WebDriver等待给定时间或直到元素可见。这称为显式等待。 See the docs。这是一些示例代码:

element = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.ID,'name_of_id'))) # you can also use xpath or any of selenium's other find_by commands.  [See here][2] for a list.

然后你需要click()按钮。它就像以下一样简单。

element.click()

那就是它。如果我误解了您的问题,请随时在评论中澄清。祝你好运!

答案 1 :(得分:0)

不确定python语法,在java中你可以通过回退到JavaScript驱动程序来点击不可见的元素,例如js.executeScript("arguments[0].click();", element);,这个解决方案的要点也应该适合你,只需在python中编写