3

I'm currently working on a project where I need Selenium to refresh two tabs at an exact time and I don't want to wait for the site to be loaded. I tried every method described in multiple posts and still don't get it right. The snippet below leads to nothing and other methods like the browser.refresh() method seem to be synchronous. browser. Execute_Script("location.reload();") also seems to be quite erratic about acting synchronous or not. The goal is to reload the multiple tabs and then figure out if a button is present or not if so, it will be clicked.

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
import datetime

browser.get('https://google.com')
time.sleep(10)
browser.find_element_by_css_selector("body").send_keys(Keys.F5)

I've also thought about using something like, directly executed into the browser. But this seems to be impossible, is it?

function ready(callback){
        // in case the document is already rendered
        if (document.readyState!='loading') callback();
        // modern browsers
        else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback);
        // IE <= 8
        else document.attachEvent('onreadystatechange', function(){
            if (document.readyState=='complete') callback();
        });
    }

ready(function(){
    console.log("DOM fully loaded and parsed");
    var result = document.evaluate("//*[text()='5544']/../../td[@class='action']/form/input[@type='submit' and not(@disabled)]", document, null, XPathResult.ANY_TYPE, null);
    result.click();
});

UPDATE: I figured out that:

browser.get('https://google.com')
time.sleep(5)
username = browser.find_element_by_xpath("//input[@type='text']")
username.click()
username.clear()
username.send_keys(Keys.Enter)

works just fine. But Keys.F5 does not refresh the page... it seems to be a bug? The main part of my question is that I want to know a workaround for this send_keys F5 operation (asynchronous).

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Ranger
  • 75
  • 7

0 Answers0