Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python + Selenium - How to check an image which is styled with CSS and displayed as content?

Details Normally a picture is gladly displayed via an ID. But in my example these images are displayed as content / character:

.fa-calendar-alt:before {
    Synchro : "\f073";

What can I do here?

like image 358
Mornon Avatar asked May 16 '20 10:05

Mornon


People also ask

How to check if an image is displayed on page using selenium?

How to check if an image is displayed on page using Selenium? How to check if an image is displayed on page using Selenium? We can check if an image is displayed on page with Selenium. To verify an image we shall take the help of Javascript Executor. We shall utilize executeScript method to execute the Javascript commands in Selenium.

How to download images with Selenium Webdriver in Python?

We can download images with Selenium webdriver in Python. First of all, we shall identify the image that we want to download with the help of the locators like id, class, xpath, and so on. We shall use the open method for opening the file in write and binary mode (is represented by wb ).

What is @is_displayed () method in selenium Python?

is_displayed () element method – Selenium Python. Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver.

What is selenium Python and how to use it?

Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout – Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful.


3 Answers

If I understood you correctly, you need to ckeck the "content" value of before pseudo-element. In this case I'd suggest you to try to do it with JS. Look here to see how to run JS code via selenium.

return document.defaultView.getComputedStyle(document.querySelector('.far.fa-calendar-alt'), ':before')['content'];

After getting the value you can do simple string comparison.

like image 168
Nestor Yanchuk Avatar answered Oct 19 '22 03:10

Nestor Yanchuk


check for the class name if it exists then execute your next step.

e.g. driver.find_element_by_class_name("far fa-calendar-alt")

or you can just define it's xpath. Let me know if you need to know how to find the xpath.

Edit: Xpath example:

//div//i[@class="far fa-calendar-alt"]

like image 45
Hassaan Ali Avatar answered Oct 19 '22 04:10

Hassaan Ali


A bit of more details about your usecase would have helped us to construct a more canonical answer. However, the desired element is applied with a A CSS pseudo-element.

Usually the Calendar elements are interactive. So to identify the Calendar element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    calendar = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "i.far.fa-calendar-alt")))
    
  • Using XPATH:

    calendar = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//i[@class='far fa-calendar-alt']")))
    
  • Note : You have to add the following imports :

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

CSS Pseudo-elements

Now, if your usecase is to extract the value of the content property of the ::before element i.e. Synchro : "\f073" you can use the following solution:

script = "return window.getComputedStyle(document.querySelector('.fa-calendar-alt'),':before').getPropertyValue('content')"
print(driver.execute_script(script).strip())

Reference

You can find a detailed discussion in:

  • How locate the pseudo-element ::before using Selenium Python
like image 4
undetected Selenium Avatar answered Oct 19 '22 04:10

undetected Selenium