硒元素不会点击

时间:2016-03-15 11:44:56

标签: python firefox selenium-webdriver

我遇到问题,.click()对元素不起作用。点击鼠标后,会弹出一个小窗口,其中包含可供选择的值。但是,即使找到了元素,我也无法点击该元素,因为它会给我带来错误。使用Selenium IDE并记录我的操作,它记录了对同一元素的单击操作,因此我不明白什么不起作用。到目前为止,我已经导航并点击了很多元素。

以下是引发错误的代码段:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.ui import Select
import time

def FirefoxTest():
    fp = webdriver.FirefoxProfile('C:/Users/MYUSER/AppData/Roaming/Mozilla/Firefox/Profiles/ethy92lt.default')
    driver = webdriver.Firefox(fp)
    site = ("www.company_intranet.com")
    driver.get(site)
    try:
        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "C29_W35_V37_V46_btresporg_struct.partner_no-btn")))
    except:
        print("C29_W35_V37_V46_btresporg_struct.partner_no-btn not found")
    driver.find_element_by_id("C29_W35_V37_V46_btresporg_struct.partner_no-btn").click()

我尝试点击元素ID:

<a class="th-ip-h" id="C29_W35_V37_V46_btresporg_struct.partner_no-btn" href="javascript:void(0)" onclick="thtmlb_hideContextMenu(event); thtmlbCSVHManger.inputfieldF4IconClick('C29_W35_V37_V46_btresporg_struct.partner_no','C29_W35_V37_V46','ZSRQM_PROB_H','PROBLEMHEADEREF');htmlbSubmitLib('htmlb',this,'thtmlb:inputField:inputField:null','myFormId','C29_W35_V37_V46_btresporg_struct.partner_no','\x23AUTONAV\x23OP_FINDRESPORG\x23BTRESPORG\x230\x23PARTNER_NO',0);return false;" onfocus="thtmlbSaveKeyboardFocus('C29_W35_V37_V46_btresporg_struct.partner_no');thtmlb_hideContextMenu();" title="Open Input Help"><img class="th-ip-img" src="/SAP/BC/BSP/SAP/ZIR_STYLES/EHP3 default/images/helpF4.gif" alt="Open Input Help"></a>

我得到错误:

Traceback (most recent call last):
  File "C:/Users/MYUSER/PycharmProjects/untitled/selen_QT.py", line 229, in <module>
    FirefoxTest()
  File "C:/Users/MYUSER/PycharmProjects/untitled/selen_QT.py", line 98, in FirefoxTest
    driver.find_element_by_id("C29_W35_V37_V46_btresporg_struct.partner_no-btn").click()
  File "C:\Users\MYUSER\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 75, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\MYUSER\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 454, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\MYUSER\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "C:\Users\MYUSER\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 102, in check_response
    value = json.loads(value_json)
  File "C:\Users\MYUSER\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Users\MYUSER\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\MYUSER\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
在解决问题之前

编辑睡眠计时器

time.sleep(5) driver.find_element_by_id("C29_W35_V37_V46_btresporg_struct.partner_no-btn").click()

1 个答案:

答案 0 :(得分:0)

try except阻止了问题,等待该元素,但即使它超时,您也会尝试找到它。它应该如下:

try:
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "C29_W35_V37_V46_btresporg_struct.partner_no-btn")))
    driver.find_element_by_id("C29_W35_V37_V46_btresporg_struct.partner_no-btn").click()
except TimeoutException:
    print("C29_W35_V37_V46_btresporg_struct.partner_no-btn not found")

虽然我不认为这是问题的根源。