Selenium webdriver无法定位元素(它不是隐藏的,它不在子帧内)

时间:2013-08-27 19:02:18

标签: python python-2.7 selenium selenium-webdriver

我需要与包含以下HTML代码的页面进行交互:

 <a data-refineby="customDateRange" class="toggleControl flushToggleControl navToggleControl">Specific Dates</a>
        <div class="navTabDropDown customDateFilter">
            <form>
                <div>
                    <span class="dateLabel">From:</span>
                    <input type="text" name="fromMonth" class="month" id="fromMonth" value="MM" maxlength="2" /> / 
                    <input type="text" name="fromDay" class="day" id="fromDay" value="DD" maxlength="2" /> / 
                    <input type="text" name="fromYear" class="year" id="fromYear" value="YYYY" maxlength="4" />
                </div>
                <div>
                    <span class="dateLabel">To:</span>
                    <input type="text" name="toMonth" class="month" id="toMonth" value="MM" maxlength="2" /> / 
                    <input type="text" name="toDay" class="day" id="toDay" value="DD" maxlength="2" /> / 
                    <input type="text" name="toYear" class="year" id="toYear" value="YYYY" maxlength="4" />
                </div>
                <div class="customFilterFooter">
                    <a class="applyCustomFilter">Refine Search</a>
                </div>
            </form>
        </div>
    </li>

这是我正在使用的(Python)代码:

# import selenium, start webdriver, etc

browser.find_element_by_id("newSearchQueryTop").send_keys("foo")
browser.find_element_by_css_selector("button.button").click()
browser.find_element_by_link_text("Specific Dates").click()
browser.find_element_by_id("fromMonth").send_keys("01")
browser.find_element_by_id("fromDay").send_keys("01")
browser.find_element_by_id("fromYear").send_keys("1992")
browser.find_element_by_id("toMonth").send_keys("12")
browser.find_element_by_id("toDay").send_keys("31")
browser.find_element_by_id("toYear").send_keys("1992")
browser.find_element_by_link_text("Refine Search").click()

webdriver正确填写日/月/年字段,但无法找到“优化搜索”按钮。这是我得到的:

Traceback (most recent call last):
  File "nyt.py", line 170, in <module>
    nyt(1, years1, 1, path)
  File "nyt.py", line 149, in nyt
    browser.find_element_by_link_text("Refine Search").click()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 246, in find_element_by_link_text
    return self.find_element(by=By.LINK_TEXT, value=link_text)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 680, in find_element
    {'using': by, 'value': value})['value']
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"link text","selector":"Refine Search"}' ; Stacktrace: 
    at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/CQ/CQ1pELFuECu0jHex28Y-tU+++TI/-Tmp-/tmpOR8AB5/extensions/fxdriver@googlecode.com/components/driver_component.js:8899)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/CQ/CQ1pELFuECu0jHex28Y-tU+++TI/-Tmp-/tmpOR8AB5/extensions/fxdriver@googlecode.com/components/driver_component.js:396) 

“优化搜索”按钮不在子框架内且未隐藏,因此我不明白为什么webdriver无法找到它。我试过time.sleep(30),但这并没有改变任何东西。有什么想法吗?

(Mac OS X 10.6.8,Python 2.7.5,Selenium 2.35,Firefox 23.01)

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

browser.find_element_by_xpath("//a[@class='applyCustomFilter' and text() = 'Refine Search']").click()

XPATH Tester