SeleniumHQ/selenium · error · WebDriverException

You can only set the orientation to 'LANDSCAPE' and 'PORTRAI

Error message

You can only set the orientation to 'LANDSCAPE' and 'PORTRAIT'

What it means

Raised by the orientation setter when value (after .upper()) is not one of the two W3C screen-orientation values LANDSCAPE or PORTRAIT. The setter compares the uppercased string against an allow-list and forwards only valid values to the SET_SCREEN_ORIENTATION command. Note: the comparison is case-insensitive, so 'landscape' is accepted; anything else is rejected.

Source

Thrown at py/selenium/webdriver/remote/webdriver.py:1142

            `orientation = driver.orientation`
        """
        return self.execute(Command.GET_SCREEN_ORIENTATION)["value"]

    @orientation.setter
    def orientation(self, value) -> None:
        """Sets the current orientation of the device.

        Args:
            value: Orientation to set it to.

        Example:
            `driver.orientation = "landscape"`
        """
        allowed_values = ["LANDSCAPE", "PORTRAIT"]
        if value.upper() in allowed_values:
            self.execute(Command.SET_SCREEN_ORIENTATION, {"orientation": value})
        else:
            raise WebDriverException("You can only set the orientation to 'LANDSCAPE' and 'PORTRAIT'")

    def start_devtools(self) -> tuple[Any, WebSocketConnection]:
        global cdp
        import_cdp()
        if self.caps.get("se:cdp"):
            ws_url = self.caps.get("se:cdp")
            cdp_version = self.caps.get("se:cdpVersion")
            if cdp_version is None:
                raise WebDriverException("CDP version not found in capabilities")
            version = cdp_version.split(".")[0]
        else:
            version, ws_url = self._get_cdp_details()

        if not ws_url:
            raise WebDriverException("Unable to find url to connect to from capabilities")

        if cdp is None:
            raise WebDriverException("CDP module not loaded")

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Use one of the two allowed values: driver.orientation = 'LANDSCAPE' or 'PORTRAIT' (case-insensitive).
  2. Normalize external input by upper-casing and mapping synonyms to LANDSCAPE/PORTRAIT before assignment.
  3. Guard against None/non-string inputs before calling the setter.

Example fix

# before
driver.orientation = 'horizontal'

# after
driver.orientation = 'LANDSCAPE'
Defensive patterns

Strategy: validation

Validate before calling

allowed = {'LANDSCAPE', 'PORTRAIT'}
if not isinstance(value, str) or value.upper() not in allowed:
    raise ValueError(f'orientation must be one of {allowed}')
driver.orientation = value

Type guard

def is_valid_orientation(v) -> bool:
    return isinstance(v, str) and v.upper() in {'LANDSCAPE', 'PORTRAIT'}

Try / catch

from selenium.common.exceptions import WebDriverException
try:
    driver.orientation = value
except WebDriverException:
    driver.orientation = 'PORTRAIT'  # safe default

Prevention

When it happens

Trigger: Calling driver.orientation = 'sideways', 'horizontal', '', or any string whose upper() is not LANDSCAPE/PORTRAIT. Note: passing None raises AttributeError on .upper() before reaching this message, so this message specifically requires a non-LANDSCAPE/PORTRAIT string.

Common situations: A developer guesses an orientation name ('horizontal', 'vertical', 'square') instead of the W3C constants. Or passes a user-supplied string without normalizing it first.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/5bfe9b7773230cc4. Report an issue: GitHub.