使用selenium webdriver

时间:2017-05-30 11:26:06

标签: python selenium css-selectors

我正在尝试使用Python使用selenium来获取id。

问题是测试主机的速度不是那么快,因此网页加载时间可能长达10-15秒。

所以,我开始使用这段代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

browser = webdriver.Chrome("C:/Users/user/Documents/chromedriver.exe")
browser.get("my_url")
delay = 20 # seconds
try:
    WebDriverWait(browser, delay).until(EC.presence_of_element_located(browser.find_element_by_id('X4')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")

但是,此代码根本不适用于这些例外:

Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/testproj/test_scripts/test.py", line 10, in <module>
    WebDriverWait(browser, delay).until(EC.presence_of_element_located(browser.find_element_by_id('X4')))
  File "C:\Program files (x86)\Python\Python36-32\lib\site-packages\selenium-3.4.2-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 285, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Program files (x86)\Python\Python36-32\lib\site-packages\selenium-3.4.2-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 787, in find_element
    'value': value})['value']
  File "C:\Program files (x86)\Python\Python36-32\lib\site-packages\selenium-3.4.2-py3.6.egg\selenium\webdriver\remote\webdriver.py", line 252, in execute
    self.error_handler.check_response(response)
  File "C:\Program files (x86)\Python\Python36-32\lib\site-packages\selenium-3.4.2-py3.6.egg\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"X4"}

简单地说:“页面上没有这样的元素”。

奇怪的是,即使在整页加载之前,堆栈跟踪也会被打印出来,所以我不知道发生了什么。

此外,我试图让其他身份证明没有运气 - 似乎与某些元素无关的问题。

以下是我正在尝试检索的元素页面的来源:

<div id="X4Edit" class="mandatoryFieldStyle xEdit file_Todo field_L_switch_inbox value_Todo" style="height:20px;">
    <div class="xEditInner" style="height:100%;">
        <input type="text" id="X4" value="Todo list" scripttype="combo" tabindex="" datachangeevent="817" 
               onkeydown="widgets.Combo.keyDown(event, 'X4');" 
               dvdvar="var/L.switch.inbox" buttonid="" reflabels="" onkeyup="widgets.Combo.keyUp(event, 'X4');" 
               alias="var/L.switch.inbox" name="var/L.switch.inbox" 
               autocomplete="off" role="combobox" aria-autocomplete="list" aria-owns="X4Popup_div" aria-activedescendant="X4Popup_div" 
               aria-haspopup="true" aria-expanded="false" maxlength="" style="
                height:18px;margin-top:1px;
                " selectonly="1" onfocus="widgets.Combo.handleOnFocus(this, event);" onblur="widgets.Combo.handleOnBlur(this, event);" 
               onclick="handleOnClick(this, event);" onchange="handleOnChange(this, event);">
    </div>
    <div id="X4ButtonDiv" class="xButton" tabindex="-1" style="">
        <a href="javascript:widgets.Combo.togglePopup('X4', true);void(0);" tabindex="-1" class="image-link" draggable="false" role="presention">
          <img id="X4Button" src="images/comboup.png" alt="" draggable="false" tabindex="-1">
        </a>
    </div>
</div>

那么,如何通过id正确检索此组合框?以下想尝试设置它的状态。

1 个答案:

答案 0 :(得分:2)

你错误地使用了ExplicitWait:在这一行

WebDriverWait(browser, delay).until(EC.presence_of_element_located(browser.find_element_by_id('X4')))

解释器尝试执行browser.find_element_by_id('X4')而无需等待。您应该将(By.ID, "some_id")之类的可迭代内容传递给EC.presence_of_element_located()而不是WebElement

尝试使用以下代码:

try:
    WebDriverWait(browser, delay).until(EC.presence_of_element_located(("id", "X4")))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")