避免访问selenium / Chrome中的阴影元素

时间:2018-04-18 08:30:09

标签: python python-3.x selenium

在我的Chrome更新(现在版本为66.0.3359.117)之后,插件选项不会像以前那样显示(您必须再次单击才能获得相同的结果,它更加丑陋,并且您所拥有的一切之前使用Selenium来访问插件选项不再适用here以及所有其他类似链接,感谢谷歌团队),因此这个问题)。

在按钮上调用click()submit()不再有效。我猜测它与'css-build': 'shadow'有关。有没有办法访问原始'id': 'paper-button'元素(而不是“阴影”元素?)

我已经制作了这个小的python3脚本来枚举并尝试通过所有可能的方式找到一个元素。唯一有效的是:

driver.find_elements_by_id("paper-button")

奇怪的是paper-button是一个元素,它不是一个元素的id ......检查员会这样说:

<paper-button id="details-button" aria-describedby="a11yAssociation" role="button" 
              tabindex="0" animated="" aria-disabled="false" elevation="0">
    Details
</paper-button>

但是当我枚举其属性时,感谢this,我得到了这个:

{'assetpath': 'chrome://resources/polymer/v1_0/paper-button/',
 'css-build': 'shadow',
 'id': 'paper-button'}

不仅菜单会让您更多地获得相同的结果,但似乎现在使用polymer来显示此菜单,因此显示'css-build': 'shadow'

from selenium import webdriver                                                   
from selenium.webdriver.common.by import By                                      
from time import sleep                                                           
from pprint import pprint                                                        

def print_err(e):                                                                
    pass  # print(str(e).split("\n")[0])                                         


def print_ok(st, res):                                                           
    if not isinstance(res, list):                                                
        print(res)                                                               
        print("NOT A LIST : ")                                                   
        print(dir(res))                                                          
        print(st, res.is_selected())                                             
        attrs = driver.execute_script(                                           
            'var items = {}; '+                                                  
            'for (index=0; index<arguments[0].attributes.length; ++index) { '+   
            'items[arguments[0].attributes[index].name] = '+                     
            'arguments[0].attributes[index].value }; '+                          
            'return items;', res)                                                
        pprint(attrs)                                                            
        return                                                                   
    if len(res):                                                                 
        print(res)                                                               
        print([x for x in res])                                                  
        print([x.get_attribute("role") for x in res])                            
        print([x.get_attribute("describedby") for x in res])                     
        print(dir(res))                                                          

def l(desc, st, fn):                                                             
    try:                                                                         
        print("driver.{}({})...".format(desc, st))                               
        print_ok(st, fn(st))                                                     
    except Exception as e:                                                       
        print_err(e)                                                             

def search(st):                                                                  
    l("find_element_by_class_name", st, driver.find_element_by_class_name)          
    l("find_element_by_css_selector", st, driver.find_element_by_css_selector)   
    l("find_element_by_id", st, driver.find_element_by_id)                       
    l("find_element_by_link_text", st, driver.find_element_by_link_text)         
    l("find_element_by_name", st, driver.find_element_by_name)                   
    l("find_element_by_partial_link_text", st, driver.find_element_by_partial_link_text)
    l("find_element_by_tag_name", st, driver.find_element_by_tag_name)           
    l("find_element_by_xpath", st, driver.find_element_by_xpath)                 
    l("find_elements_by_class_name", st, driver.find_elements_by_class_name)        
    l("find_elements_by_css_selector", st, driver.find_elements_by_css_selector) 
    l("find_elements_by_id", st, driver.find_elements_by_id)                     
    l("find_elements_by_link_text", st, driver.find_elements_by_link_text)          
    l("find_elements_by_name", st, driver.find_elements_by_name)                 
    l("find_elements_by_partial_link_text", st, driver.find_elements_by_partial_link_text)
    l("find_elements_by_tag_name", st, driver.find_elements_by_tag_name)         


driver = webdriver.Chrome()                                                      
driver.get("chrome://extensions-frame")                                          
search("paper-button")                                                           
search("details-button")                                                         
search("#details-button")                                                        
search('//[@id="details-button"')    

1 个答案:

答案 0 :(得分:0)

要获得带阴影穿孔选择器的第一个Detail按钮:

driver.get("chrome://extensions/")
bt_detail = driver.find_element_by_css_selector("* /deep/ #details-button")

或者从所有扩展程序中获取所有信息:

driver.get("chrome://extensions/")
data = driver.execute_async_script("chrome.management.getAll(arguments[0])")
print(data)