Cennnn
Cennnn

Reputation: 45

How do I target a div with a certain style with Selenium in Python?

Hi i'm trying to target a color picker pop up. Selenium can't find the elements in the picker and I think it has to do with the fact that there a lot of the same divs in the code of the site.

My thinking was that i'd have to select by style, as that is the only thing that's different.(see screenshot)

But I can't it to work on selecting by style

I've tried via Xpath and by CSS selector. But I must be doing something wrong.

What I have now is:


driver.find_element_by_class_name("sp-replacer").click()

driver.find_element_by_css_selector(".div[style='position: absolute; top: 721.203px; left: 0px;']")

enter image description here

Upvotes: 0

Views: 4339

Answers (3)

Janib Soomro
Janib Soomro

Reputation: 622

Try something like:

//div[contains(@class,'some_wanted_class') and contains(@class,'other_wanted_class') and not(contains(@class,'some_unwanted_term_in_class'))]

Upvotes: 3

KunduK
KunduK

Reputation: 33384

Use following CSS Selector.

element=driver.find_element_by_css_selector('div.sp-container.sp-light[style="position: absolute; top: 721.203px; left: 0px;"]')

To handle dynamic element use WebdriverWait with CSS selector locator.

element=WebDriverWait(driver,40).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,'div.sp-container.sp-light[style="position: absolute; top: 721.203px; left: 0px;"]')))

Please note you need to have following imports

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

Upvotes: 3

supputuri
supputuri

Reputation: 14135

Here is the xpath I would rather use, as the class name different.

//div[@class='sp-container sp-light sp-buttons-disabled sp-palette-disabled']

Upvotes: 1

Related Questions