ADADBA
ADADBA

Reputation: 33

How to locate style element in Selenium Python?

Hello I want to locate a style element / style elements with Selenium Python,

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

I tried it in ways like:

self.driver.find_elements_by_xpath("//div[@class='flex-direction:column;padding-bottom:835px;padding-top:0px;']/div")

But it does not work. So how do I locate these elements using Selenium Python?

Upvotes: 1

Views: 1824

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193058

The HTML is of a <div> element which predominantly contains the <style> attribute for it's descendant(s). It would be highly undesirable to locate element/elements based on only the <style> attribute.


Solution

Instead it would be better to move to the descendant and locate the element(s) with respect to their (common) attributes. As an example:

  • Using class_name:

    elements = driver.find_elements(By.CLASS_NAME, "class_name")
    
  • Using id:

    elements = driver.find_elements(By.ID, "id")
    
  • Using name:

    elements = driver.find_elements(By.NAME, "name")
    
  • Using link_text:

    elements = driver.find_elements(By.LINK_TEXT, "link_text")
    
  • Using partial_link_text:

    elements = driver.find_elements(By.PARTIAL_LINK_TEXT, "partial_link_text")
    
  • Using tag_name:

    elements = driver.find_elements(By.TAG_NAME, "tag_name")
    
  • Using css_selector:

    elements = driver.find_elements(By.CSS_SELECTOR, "css_selector")
    
  • Using xpath:

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

Upvotes: 0

KunduK
KunduK

Reputation: 33384

The provided HTML has no class attribute. However in your xpath you have provided class attribute it should be style attribute.

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

Ideally your code should be

self.driver.find_elements_by_xpath("//div[@style='flex-direction: column; padding-bottom: 65px; padding-top: 0px;']")

Upvotes: 2

Related Questions