Webdriver (python)中调用JS

python

  1、Webdriver 中调用JS,由于页面中的input是被隐藏起来的,所以必须用JS把隐藏的元素显示出来;

       browser.find_element_by_xpath("//div[@id=\'surveyItemsWrap\']/div/div[2]/button[2]").click()
        time.sleep(5)
        inputs=browser.find_element_by_xpath("//body/input")
        js="var q=document.getElementByClassName(\'ts_bg alert\');q.style.display=\'block\';"
        #  setAttribute(\"style\",\"display:block\     
        print "js"
        browser.execute_script(js)
        browser.find_element_by_xpath("//body/input").send_keys(\'F:\\Img\2.jpg\')
        print "ok"

 

报错:

Traceback (most recent call last):

File "E:\eclipse\PythonCase\src\Survey_test\LogicMore.py", line 85, in test_Surveyor

browser.execute_script(js)

File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 396, in execute_script

{\'script\': script, \'args\':converted_args})[\'value\']

File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 164, in execute

self.error_handler.check_response(response)

File "D:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 164, in check_response

raise exception_class(message, screen, stacktrace)

WebDriverException: Message: u"unknown error: Object #<HTMLDocument> has no method \'getElementByClassName\'\n (Session info: chrome=28.0.1500.72)\n (Driver info: chromedriver=2.3,platform=Windows NT 6.1 SP1 x86_64)"

PS: JS有待加深学习!

 

以上是 Webdriver (python)中调用JS 的全部内容, 来源链接: utcz.com/z/388278.html

回到顶部