简体   繁体   中英

Selenium-python make invisible element visible

I am using selenium-python bindings to perform some basic browser testing. I need to click an element, which is invisible by default. I have checked it by is_displayed() method . So, I have tried to use execute_script method to make it visible by using the following code, but I am getting error messages. Its a common scenario where we need to make an invisible element visible. There must be some other ways to circumnavigate this kind of problems. It will be helpful if anybody point me out the problem in my code. Can I click on an element with pure js within a python code?

print "getting keyword ideas"
searches = driver.find_element_by_xpath("//*[contains(text(), 'Avg. monthly searches')]")
driver.execute_script("arguments.style.visibility='visible';", searches)

and the error message:

Traceback (most recent call last): File "C:\\vhosts\\phpsols\\splinter\\adwords.py", line 140, in driver.execute_script("arguments.style.visibility='visible';", searches) File "C:\\anaconda32\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py", line 429, in execute_script {'script': script, 'args':converted_args})['value'] File "C:\\anaconda32\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py", line 201, in execute self.error_handler.check_response(response) File "C:\\anaconda32\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py", line 181, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: {"errorMessage":"undefined is not an object (evaluating 'arguments.style.visibility='visible'')","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"210","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:55867","User-Agent":"Python -urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"sessionId\\": \\"8f05e120-f672-11e5-91c5-c7097c43ddb4\\", \\"args\\": [{\\"element-6066-11e4-a52e-4f735466cecf\\": \\":wdc:1459340795815\\", \\"ELEMENT\\": \\":wdc:1459340795815\\"}], \\"script\\": \\"arguments.style.visibility='visible';\\"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/8f05e120-f672-11e5-91c5-c7097c43ddb4/execute"}} Screenshot: available via screen

<div id="gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0">
    <div style="text-align:right">Avg. monthly searches
        <span class="table-tooltip" id="gwt-debug-tooltip-4580983">&nbsp;</span>
        <br>
        </div>
    </div>

Here is the python code:

print "getting keyword ideas"
driver.execute_script('document.getElementById("gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0").style.visibility = "visible";')
driver.execute_script('document.getElementById("gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0").style.display = "block";')
searches = driver.find_element_by_xpath("//*[contains(text(), 'Avg. monthly searches')]")
if searches.is_displayed():
    print "searches is visible"
else:
    print "searches isn't visible"
avg_monthly_searches = driver.find_elements_by_id("gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0") 
for avg in avg_monthly_searches:
    if avg.is_displayed():
        print "element is visible, so clicking ..."
        actions = ActionChains(driver)
        actions.click(on_element=avg).perform()
        time.sleep(10)
        print "scrolling to the bottom"
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        print "getting the page source as interpreted by chromedriver"
        driver.execute_script('return document.documentElement.innerHTML')
        print "getting keyword ideas source"
        content = driver.page_source
        with open('keyword_ideas.html', 'w') as f:
            f.write(content.encode('utf-8'))    
            time.sleep(5)
            print "getting html"
            dom = DOM(content)
            print "traversing ... "
            for e in dom('td.spgb-f'):
                for a in e('a.sptc-e.sptc-h'):
                    print repr(plaintext(a.content))           
    else:
        print "element isn't visible"

Try following code:

driver.execute_script('document.getElementById("gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0").style.visibility = "visible";')
driver.execute_script('document.getElementById("gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0").style.display = "block";')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM