如何在Selenium Python中定位样式元素?

如何在Selenium Python中定位样式元素?,python,html,selenium,selenium-webdriver,xpath,Python,Html,Selenium,Selenium Webdriver,Xpath,您好,我想用Selenium Python查找样式元素/样式元素 <div style="flex-direction: column; padding-bottom: 65px; padding-top: 0px;"> 但它不起作用。那么如何使用Selenium Python定位这些元素呢?提供的HTML没有类属性。但是,在xpath中,您提供了class属性,它应该是style属性 //div[@style='flex-direction: column;

您好,我想用Selenium Python查找样式元素/样式元素

<div style="flex-direction: column; padding-bottom: 65px; padding-top: 0px;">

但它不起作用。那么如何使用Selenium Python定位这些元素呢?

提供的HTML没有
属性。但是,在
xpath
中,您提供了
class
属性,它应该是
style
属性

//div[@style='flex-direction: column; padding-bottom: 65px; padding-top: 0px;']
理想情况下,您的代码应该是

self.driver.find_elements_by_xpath("//div[@style='flex-direction: column; padding-bottom: 65px; padding-top: 0px;']")
HTML是一个
元素,主要包含其后代的
属性。仅基于
属性定位元素是非常不可取的

//div[@style='flex-direction: column; padding-bottom: 65px; padding-top: 0px;']

解决方案 相反,最好移动到子体,并根据它们的(公共)属性定位元素。例如:

  • 使用
    类名

    elements = driver.find_elements(By.CLASS_NAME, "class_name")
    
    elements = driver.find_elements(By.NAME, "name")
    
    elements = driver.find_elements(By.TAG_NAME, "tag_name")
    
  • 使用
    id

    elements = driver.find_elements(By.ID, "id")
    
  • 使用
    名称

    elements = driver.find_elements(By.CLASS_NAME, "class_name")
    
    elements = driver.find_elements(By.NAME, "name")
    
    elements = driver.find_elements(By.TAG_NAME, "tag_name")
    
  • 使用
    链接文本

    elements = driver.find_elements(By.LINK_TEXT, "link_text")
    
    elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "partial_link_text")
    
  • 使用部分链接文本

elements = driver.find_elements(By.LINK_TEXT, "link_text")
elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "partial_link_text")
  • 使用
    标记\u名称

    elements = driver.find_elements(By.CLASS_NAME, "class_name")
    
    elements = driver.find_elements(By.NAME, "name")
    
    elements = driver.find_elements(By.TAG_NAME, "tag_name")
    
  • 使用
    css\u选择器

    elements = driver.find_elements(By.CSS_SELECTOR, "css_selector")
    
  • 使用
    xpath

    elements = driver.find_elements(By.XPATH, "xpath")