硒蟒蛇使不可见元素可见

Selenium-python make invisible element visible

本文关键字:元素      更新时间:2023-10-11

我正在使用selenium-python绑定来执行一些基本的浏览器测试。我需要单击一个默认情况下不可见的元素。我已通过is_displayed() method进行了检查。因此,我尝试使用execute_script方法,通过使用以下代码使其可见,但我收到了错误消息。这是一种常见的场景,我们需要使一个不可见的元素可见。必须有一些其他方法来绕过这类问题。如果有人指出我代码中的问题,那将很有帮助。我可以在python代码中用纯js点击一个元素吗?

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

以及错误消息:

追踪(最近一次通话):文件"C:''vhosts''phpsols''split''addwords.py",第140行,位于driver.execute_script("arguments.style.visibility='visible';",搜索)文件"C:''anaconda32''lib''site packages''selenium''webdriver''remote''webdriver.py",execute_script中的第429行{'script':脚本,args:converted_args})["value"]文件"C:''anaconda32''lib''site packages''selenium''webdriver''remote''webdriver.py",第201行,执行中self.error_handler.check_response(响应)文件"C:''anaconda32''lib''site packages''selenium''webdriver''remote''errorhandler.py",第181行,在check_response中引发exception_class(消息、屏幕、堆栈)selenium.com.mon.exceptions.WebDriverException:消息:{"errorMessage":"未定义不是对象(正在评估arguments.style.visibility='visible')","request":{"headers":{"Accept":"application/json","Accept Encoding":"identity","Connection":"close","Content-Length":"210","Content-Type":"applications/json;charset=UTF-8","Host":"127.0.0.1:55867","User Agent":"Python urllib/2.7"},"httpVersion":"1.1","method":"POST","POST":''"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":":["execute"]},"urlOriginal":"/session/8f05e120-f672-11e5-91c5-c7097c43ddb4/execute"}}"屏幕截图:可通过屏幕获得

<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>

以下是python代码:

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"

尝试以下代码:

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";')