Home > Blockchain >  Selenium giving error when window in different aspect ratio
Selenium giving error when window in different aspect ratio

Time:10-01

So I am trying to scrape https://www.iob.in/Branch.aspx and want to go to different pages and was using selenium for it. It works well on wider screen but on taller screens, the atm/branch button comes above pages 6/7 and selenium cant figure them out or scroll to reach. Any way around it or how to fix it? I tried starting in maximised mode or setting a fixed window size but ut doesn't resolve the issue

    options.add_argument("window-size=1900,900")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("useAutomationExtension", False)
    driver = webdriver.Chrome(
        options=options,
    )
    driver.get("https://www.iob.in/Branch.aspx")
    blah = []
    for page in range(2, 10):
        try:

            WebDriverWait(driver, 20).until(
                EC.visibility_of_element_located(
                    (
                        By.XPATH,
                        f"//*[@id='ctl00_ContentPlaceHolder1_gv_Branch']/tbody/tr[22]/td/table/tbody/tr/td[{page}]/a",
                    )
                )
            ).click()

CodePudding user response:

Selenium does not click the element if it is not inside the page's visible area. You can use javascript to click the element, even it's not in visible area, like:

driver.implicitly_wait(20)  # just an alternate to WebDriverWait, but it's value is set for whole life of the program until you change it

....
for page in range(2, 10):
    element = driver.find_element_by_xpath(f"//*[@id='ctl00_ContentPlaceHolder1_gv_Branch']/tbody/tr[22]/td/table/tbody/tr/td[{page}]/a")
    driver.execute_script("arguments[0].click()", element)  # worked with small screen.

CodePudding user response:

if you just want that selenium adjust the height to page, you can use this:

height = driver.execute_script("return document.body.scrollHeight")
driver.set_window_size(1056, height)
  • Related