0

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

supputuri
  • 13,644
  • 2
  • 21
  • 39
Cennnn
  • 45
  • 1
  • 7
  • Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. Also, we need to be able to see inside of each of those DIVs (or at least a representative sample of them) so that we can give you a good locator. – JeffC May 01 '19 at 21:31
  • From your comments below, it looks like you found the answer to your own question. Please post what worked for you and make sure you mark it as the answer. Also, upvote any answers that you found helpful, even if they didn't solve your problem. – JeffC May 01 '19 at 21:34

3 Answers3

3

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
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Hi! Thank you so much for responding. I've tried it but unfortunately selenium can't find the element: Message: no such element: Unable to locate element: {"method":"css selector","selector":".div[style='position: absolute; top: 721.203px; left: 0px;']"} I've tried the dynamic element approach as well, this gives a time-out error – Cennnn May 01 '19 at 11:46
  • @Cennnn:can you try now – KunduK May 01 '19 at 14:15
  • Thank you again, I did it again but unfortunately it gives me the same results – Cennnn May 01 '19 at 15:34
  • I figured it out! I had to target the main body (that it was in) and I needed to exclude 'sp-hidden' class, that way only the right div was targeted. So the xpath is: //body[@class='portfolio upload-work create lang-en']//*[contains(@class, 'sp-container sp-light sp-buttons-disabled sp-palette-disabled') and not(contains(@class, 'sp-hidden'))]//*[@class='sp-input']") Thank you for your help! – Cennnn May 01 '19 at 19:45
3

Try something like:

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

Janib Soomro
  • 446
  • 6
  • 12
1

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']
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Hi! Thank you for helping! Unfortunately I already tried that, I did it again now though. But still Selenium cannot find it. – Cennnn May 01 '19 at 15:33
  • Are you getting no such element exception? Does the element present in the iframe by any chance? – supputuri May 01 '19 at 15:35
  • Check my answer here for the logic to [check if the element present in the iframe](https://stackoverflow.com/questions/55536851/is-there-a-way-to-search-webelement-on-a-main-window-first-if-not-found-then-s/55537186#comment97778812_55537186) – supputuri May 01 '19 at 15:39
  • I figured it out! I had to target the main body (that it was in) and I needed to exclude 'sp-hidden' class, that way only the right div was targeted. So the xpath is: //body[@class='portfolio upload-work create lang-en']//*[contains(@class, 'sp-container sp-light sp-buttons-disabled sp-palette-disabled') and not(contains(@class, 'sp-hidden'))]//*[@class='sp-input']") Thank you for your help! – Cennnn May 01 '19 at 19:45