-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] is there a way to get element background colour ? #4282
Comments
Hey @tanneerumahesh! const element = await page.waitForSelector(selector);
const color = await element.evaluate((el) => {
return window.getComputedStyle(el).getPropertyValue('background-color');
});
console.log(color); Be aware of this |
Feature request in #11494 |
For some reason, doing @viraxslot solution gives me a color that is an empty object. I am doing Does his solution work for anyone else? |
@isa3bel Hi, I've just made a fresh example with the import {test, expect} from "@playwright/test"
test("basic test", async ({page}) => {
await page.goto("https://playwright.dev/docs/intro")
const docsLink = page.locator("a >> text=Docs")
const color = await docsLink.evaluate((e) => {
return window.getComputedStyle(e).getPropertyValue("color")
})
expect(color).toBe("rgb(69, 186, 75)")
}) I believe it should check the color of this link: Please pay attention that you have a typo in |
I was wondering, why is the await keyword needed in this case? I think my program hangs if I add that keyword. Perhaps a promise is never being met? @viraxslot |
@isa3bel Well, due to playwright docs And about So it's hard to say what's wrong in your case. Could you please provide a reproducible example? |
Hi guys, how to get element background color using playwright java? |
@shilpa32 did u find any method for background color in java?? |
System.out.println(page.locator("body").evaluate("element => getComputedStyle(element)['background-color']")); |
this works for me in C# and should also work for Java, await Expect(locator).ToHaveCSSAsync("background-color", "rgb color here"); |
Has anyone run into this issue: export const getStyle = async ( locator: Locator, property: string): Promise<string> => {
return locator.evaluate((el) => window.getComputedStyle(el).getPropertyValue(property) );
};
getStyle(page.getByRole('button'), 'background-color');
// ERROR: locator.evaluate: ReferenceError: property is not defined Why is it not able to read |
@jwerre evaluation happens in browser, so you have to manually pass the value to it, something like this
|
I have a similar case, and I'm also using .net version. But it doesn't work and I get stack overflow exception :/ |
you just have to pass the element locator and the expected color.
|
So the evaluate method won't work? I don't want to assert it straight away. I need value as this is some nested object. Maybe I'm missing something, I tried to do simple check because I thought that maybe with my page is something weird happening but with this code I have same issue, stack overflow. `var playwright = await Playwright.CreateAsync();
|
Did you find any solution? I faced with the same issue. The solution, provided by phungleson doesn't work, still see the error |
@InTouch1988, @phungleson solution does work. Here's what I have (Typescript) /**
* Retrieve a computed style for an element.
*
* @function getStyle
* @async
* @param locator {Locator} The Playwright locator to evaluate (see: https://playwright.dev/docs/locators)
* @param property {string} The CSS property for the style to retrieve
* @returns Promise<string> The style value
*/
export const getStyle = async (locator: Locator, property: string): Promise<string> => {
return locator.evaluate( (el, property) => window.getComputedStyle(el)
.getPropertyValue(property), property );
};
test('get button background color', async ({ page }) => {
cost btnBgColor = await getStyle(page.getByRole('button'), 'background-color');
expect(btnBgColor).toBe('rgb(255, 0, 0)')
}); |
Working solution in C#:
|
Great thanks @GStasiakVolue public string GetElementBackgroundColor(ILocator locator) |
I'm not too sure but as of 29th January 2024 you can make use of the await expect(this.page.locator('body')).toHaveCSS('background-color', 'rgb(250, 250, 250)') |
In Playwright for Python: _button = page.get_by_role(...)
_button_style = button.evaluate("el => window.getComputedStyle(el)")
print(_button_style['backgroundColor']) # returns 'rgb(105, 105, 255)' |
You can try this: |
Thank you! you save my day! |
Hi guys, is there a way to get element background colour using playwright?
Tahnks
The text was updated successfully, but these errors were encountered: