Python Html宽度和selenium窗口大小不同

Python Html宽度和selenium窗口大小不同,python,selenium,selenium-chromedriver,google-chrome-headless,Python,Selenium,Selenium Chromedriver,Google Chrome Headless,我有个问题。我试图测试一个组件的广告宽度,以验证插槽的尺寸是否正确 from django.contrib.staticfiles.testing import StaticLiveServerTestCase from selenium import webdriver from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager

我有个问题。我试图测试一个组件的广告宽度,以验证插槽的尺寸是否正确

from django.contrib.staticfiles.testing import StaticLiveServerTestCase

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

class MobileAdTestCase(StaticLiveServerTestCase):

    def setUp(self):

        # Create chrome browser with specific configuration
        chrome_options = Options()  
        chrome_options.add_argument("--headless")
        self.browser = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
        self.browser.set_window_position(0, 0)
        self.browser.set_window_size(320,568)
        self.browser.implicitly_wait(10)

        # Verify that browser has the specified size
        browser_size = self.browser.get_window_size()
        if browser_size['width'] != 320 or browser_size['height'] != 568:
            self.fail("The browser didn't resize to the specified size.")
    
    def tearDown(self):
        self.browser.quit()

    def test_ad_display(self):
        # Given an url
        url = "127.0.0.1:8080/ads"

        # When visiting the page
        self.browser.get(url)

        # Assert that the ad has the right width
        ad = self.browser.find_element_by_id("ad")
        ad_width = ad.value_of_css_property('width')
        ad_width = int(ad_width.replace('px', ''))
        self.assertGreaterEqual(ad_width, 300)
当我运行
python3 manage.py runserver
并输入测试中指定的url时,组件的宽度为300px,但运行测试时,组件的宽度为285px。最奇怪的是html(
self.browser.find_element_by_tag_name('html')
)的宽度是305 px,但当我在浏览器中打开chrome devtools时,它的宽度是320 px(100%宽度)

有人能帮我找出问题出在哪里吗?谢谢

编辑:

我发现问题是因为元素不是100%宽度,所以如果我将浏览器大小设置为320px,则元素不是100%。有人知道为什么我必须强制html元素具有100%的宽度吗