1

I'm trying to test the video dimensions using the values in CSS as shown in the image below.

html

The CSS is being used by the Div below. The below is what I've done so far and the error I get.

def test_video_dimensions(browser):

browser.get(url_for("main.index", _external=True) + "video/title1/?video-height=500&video-width=750")

rows = browser.find_elements(
    By.XPATH, "/html/head/style[2]"
)
assert (
    rows[0].find_element(By.XPATH, "/html/head/style[2]").text
    == "CSS Value"
)

enter image description here

Any help to access the inside of the CSS style tag and compare the values in it?

5
  • when you write .text it is going to give you text between tags right ? Now what is == "CSS Value" ? Is it like you wanna get css value of the mentioned web element ? Commented Jun 23, 2021 at 5:37
  • @cruisepandey What I basically want is, The page Div adapts to the video width and height I give. The only place the values are visible is inside the Style Tag under video-dimensions. I want to assert the value of width which is 760 px to the value I give which is also 760. Commented Jun 23, 2021 at 5:40
  • You can probably go to dev tools - (by pressing F12) then -> elements -> do ctrl +f -> and then search this //style[@class= 'vjs-styles-dimensions'] and see if it's getting highlighted or not. Commented Jun 23, 2021 at 5:48
  • Yes it does get highlighted when i do that @cruisepandey Commented Jun 23, 2021 at 5:51
  • and I believe there's 1/1 entry ? Commented Jun 23, 2021 at 5:52

1 Answer 1

1

try the below code :

some_value = driver.find_element_by_xpath("//style[2]").get_attribute('innerHTML')
print(some_value)

output :

.video-dimensions {
        width: 300px;
        height: 168.75px;
      }

      .video-dimensions.vjs-fluid {
        padding-top: 56.25%;
      }

Now if you just want to get width value probably the below code should work for you :

some_value = driver.find_element_by_xpath("//style[2]").get_attribute('innerHTML').strip()
arr = some_value.split(":")
ar = arr[1].split(";")
print(ar[0])

or

some_value = driver.find_element_by_xpath("//style[2]").get_attribute('innerHTML').strip()
arr = some_value.split(":")[1].split(";")[0]
print(arr)

should print 300px in this case.

Sign up to request clarification or add additional context in comments.

9 Comments

I tried the 1st option for the Div tag as follows but still the same error. ` assert ( browser.find_element(By.XPATH, "/html/body/div[1]").value_of_css_property('video-dimensions') == "Any value" ) ` E AssertionError: assert '' == 'Any value'
Do not do assert as of now..just try to print browser.find_element(By.XPATH, "/html/body/div[1]").value_of_css_property('video-dimensions') and see what you get
The value is null.
can you try with this //style[@class= 'vjs-styles-dimensions'] ? like browser.find_element(By.XPATH, "//style[@class= 'vjs-styles-dimensions']").value_of_css_property('video-dimensions')
Still it's null. gives an empty value
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.