Selenium and :Hover CSS

xpath for capturing hover css property from selenium webelement in python

You can get the background-color, color, text-decoration or similar related CSS properties with value_of_css_property():

webElement.value_of_css_property('background-color')
webElement.value_of_css_property('color')
webElement.value_of_css_property('text-decoration')

Based on this, we can make a function that would get the CSS properties, hover an element and assert the CSS properties changed:

from selenium.webdriver.common.action_chains import ActionChains

def get_properties(element):
return {
prop: element.value_of_css_property(prop)
for prop in ['background-color', 'color', 'text-decoration']
}

def is_hovered(driver, element):
properties_before = get_properties(element)

ActionChains(driver).move_to_element(element).perform()

properties_after = get_properties(element)
return properties_before != properties_after

Usage:

button = driver.find_element_by_id("#mybutton")
is_hovered(driver, button)

How to perform mouseover function in Selenium WebDriver using Java?

Its not really possible to perform a 'mouse hover' action, instead you need to chain all of the actions that you want to achieve in one go. So move to the element that reveals the others, then during the same chain, move to the now revealed element and click on it.

When using Action Chains you have to remember to 'do it like a user would'.

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here"))).click().build().perform();

How to hover over an item avoiding a click on it using Selenium and C#

To hover over a certain button but to avoid clicking it you have to induce WebDriverWait for the desired ElementIsVisible() and the use Actions Class methods as follows:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
var element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//a[normalize-space()='Wyloguj']")));

Actions action = new Actions(driver);
action.MoveToElement(element).Perform();


References

You can find a couple of relevant detailed discussions in:

  • Scrape website with dynamic mouseover event
  • How to mouse hover using java through selenium-webdriver and Java

How to get the hover button background color of a website using selenium?

To retrieve the hover button background color of the WebElement using Selenium first you need to Mouse Hover inducing WebDriverWait for the visibility_of_element_located() and then use value_of_css_property() and you can use the following Locator Strategies:

Code Block:

driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.inshoes.gr/andrikes-zones-andrikes-zones-dermatines-diplis-opseos-33000481-kamel-mavro")
basket = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#addtocartbutton-7531")))
ActionChains(driver).move_to_element(basket).click().perform()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#addtocartbutton-7531"))).value_of_css_property('background-color'))

Console Output:

rgba(242, 35, 65, 1)

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

Hover list of elements - Selenium Java WebDriver

I've just tested a solution, but it is very crude. It works, however.

Note: navigating directly to the fifth star (the element with text "5") didn't work for me. It seems you need to hover such that the rating holder box opens, and then hover to the fifth star so that you get all of them as class="hover".

This is what I did:

-- navigate to the element above ("Write a review") with Actions

-- move down (positive "y") in increments of 1 pixel

-- after every increment, test if the element with class "wh-rating-choices" contains string "block"

-- if it does, move to element with text "5" contained under the element with class "wh-rating-choices-holder"

I tested it in python, but here is what should work in Java:

Actions action = new Actions(driver);
int inc = 0;
while (inc < 100) {
WebElement top = driver.findElement(By.xpath("//*[contains(text(), 'Write a Review')]"));
action.moveToElement(top, 0, inc).contextClick().perform();
Thread.sleep(200);
a = driver.findElement(By.xpath("//*[contains(@class, 'wh-rating-choices')]"));
if (a.getAttribute("style").contains("block") {
aa = driver.findElement(By.xpath("//*[contains(@class, 'wh-rating-choices-holder')]"));
bb = aa.findElement(By.xpath(".//*[contains(text(), '5')]"));
action.moveToElement(bb).perform();
break;
}
inc++;
}
System.out.println(bb.getAttribute("outerHTML"));

Thread.sleep(200) could be overkill, try something lower, like 50 or 20.

PS. It is possible you will need to close the pop-up first, the one that has class="af-icon-cross"



Related Topics



Leave a reply



Submit