I want to clear browsing history before open the browser - python

I have put code on selenium, where browser get refreshed after 20 second. However I want to put some more code on it, that before opening the browser it will clear history, cache and after whole work, it will get close close and again open and clear history, cache and cookies here is my code :
import time from selenium
import webdriver as wd
chromedriver - r"D:\bot\chromedriver.exe"
driver=wd.chrome(executable_path=chromedriver)
driver.get("http:\youtu.be/RsWCo_xGXxY")
refresh_rate = 20
while True:
time.sleep(refresh_rate)
driver.refresh()

To clear all browser history you can use this code:
from selenium import webdriver
import time
chrome_path = r"D:\bot\chromedriver.exe"
driver = webdriver.Chrome(chrome_path) # creates new webdriver instance
driver.get("http:\youtu.be/RsWCo_xGXxY")
driver.delete_all_cookies() # deletes all coockies
refresh_rate = 20
while True:
time.sleep(refresh_rate)
driver.refresh()
Output:

Related

the page closes as soon as it opens using webdriver

enter image description here
code:
chrom_driver_pat = "C:\pythondriver\chromedriver"
driver = webdriver.Chrome(executable_path=chrom_driver_pat)
driver.get("https://instagram.com/%22)
terminal:
c:\Users\arda6\Desktop\pyhton_kursu\selenium\setup.py:7: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path=chrom_driver_pat)
DevTools listening on ws://127.0.0.1:56927/devtools/browser/7ce68db7-9314-4927-9718-ca24f43ec2e0
When I run the code here, the instagram site should open and stay open, but when I run it, the site closes as soon as it opens. What should I do?
the page closes as soon as it opens
It shouldn't stay open with the code you've provided.
For example:
driver.get("https://instagram.com/%22)
Should be:
driver.get("https://instagram.com/%22")
There is one more thing you should do to make sure it works:
from time import sleep
#your code
#here
sleep(15)
This will make sure the code doesn't stop working after it's done what you wanted it to do for another 15 seconds.
If you want to keep the Chrome Tab alive after code execution. Consider to use Options.
from selenium.webdriver.chrome.options import Options
# Don't Close Chrome
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
# Initialize Chrome
driver = webdriver.Chrome(options=chrome_options)

Selenium Chrome Driver not able load website

Chromedriver is launched correctly, then start loading the page, the URL changes to 'http://app1.nmpa.gov.cn/?CbSlDlH0=qGk8rqrP3bxP3bxP39Exb4.QiGYTsZaK4uvCaZ_lRVZqqDE' however the page remains blank ( white).
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(executable_path="D:\Python\chromedriver.exe")
driver.maximize_window()
url = 'http://app1.nmpa.gov.cn/'
driver.get(url)
wait = WebDriverWait(driver, 60)
time.sleep(5)
driver.quit()
I tried it, I get redirected to the page "http://app1.nmpa.gov.cn/data_nmpa/face3/dir.html" which looks like this:
I did it with the firefox browser and geckodriver. Can you try and see if the problem still exists?

Why can't I drag and drop in Selenium Chromedriver with Python?

I can't drag and drop in Selenium with the latest Chromedriver.
selenium='3.141.0'
python 3.7
Chrome = 74.0.3729.169
ChromeDriver =latest
The below code executed successfully, but the items are not being dragged from source to destination. I am also not getting any error at all. I tried all of the below solutions, one by one, but none of working them are at all.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
cd = webdriver.Chrome('Chromedriver.exe')
cd.get('https://www.seleniumeasy.com/test/drag-and-drop-demo.html')
cd.maximize_window()
elements = cd.find_element_by_id('todrag')
drag_item = elements.find_elements_by_tag_name('span')
drag_to = cd.find_element_by_id('mydropzone')
# Solution 1 (not working)
for i in drag_item:
action = ActionChains(cd)
action.drag_and_drop(i, drag_to).perform() # this is not working
# Solution 2 (not working)
ActionChains(cd).click_and_hold(i).move_to_element(drag_to).release(
drag_to).perform()
# Solution 3 (not working, as you need to download the js files)
jquery_url = "http://code.jquery.com/jquery-1.11.2.min.js"
with open("jquery_load_helper.js") as f:
load_jquery_js = f.read()
with open("drag_and_drop_helper.js") as f:
js = f.read()
cd.execute_async_script(load_jquery_js, jquery_url)
cd.execute_script(js + "$(\'arguments[0]\').simulateDragDrop({ dropTarget: \"arguments[1]\"});", i, drag_to)
I think there is something wrong with the site because this example I found on the web seems to work:
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
# Create chrome driver.
driver = webdriver.Chrome()
# Open the webpage.
driver.get("https://openwritings.net/sites/default/files/selenium-test-pages/drag-drop.html")
# Pause for 5 seconds for you to see the initial state.
time.sleep(5)
# Drag and drop to target item.
##################################
drag_item = driver.find_element_by_id("draggable")
target_item = driver.find_element_by_id("droppable")
action_chains = ActionChains(driver)
action_chains.drag_and_drop(drag_item, target_item).perform()
##################################
# Pause for 10 seconds so that you can see the results.
time.sleep(10)
# Close.
driver.quit()
Hopefully, that example helped you!

Reopen same browser window using selenium python and Firefox

Hey i'm trying to make an automatic program to send Whatsapp messages.
I'm currently using python, Firefox and selenium to achieve that.
The problem is that every time i'm calling driver.get(url) it opens a new instance of the firefox browser, blank with no memories of the last run. It makes me scan the bar code every time I run it.
from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxProfile
cp_profile = webdriver.FirefoxProfile("/Users/Hodai/AppData/Roaming/Mozilla/Firefox/Profiles/v27qat5d.whatsapp_profile")
driver = webdriver.Firefox(executable_path="/Users/Hodai/Desktop/geckodriver",firefox_profile=cp_profile)
driver.get('http://web.whatsapp.com')
#Scan the code before proceeding further
input('Enter anything after scanning QR code')
I've tried to use profile but it seems like it has no affect.
cp_profile = webdriver.FirefoxProfile("/Users/Hodai/AppData/Roaming/Mozilla/Firefox/Profiles/v27qat5d.whatsapp_profile")
driver = webdriver.Firefox(executable_path="/Users/Hodai/Desktop/geckodriver",firefox_profile=cp_profile)
At the end I used chromedriver to achive my goal.
I tried cookies with pickle but it was a bit tricky because it remembered the cookies just for the same domain.
So I used user data for chrome.
now it works like a charm. thank you all.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:/Users/Designer1/AppData/Local/Google/Chrome/User Data/Profile 1")
driver = webdriver.Chrome(chrome_options=options,executable_path="C:\webdrivers\chromedriver.exe")
The easiest way I think is to save your cookies after scanned the qrcode and push them to Selenium manually.
# Load page to be able to set cookies
driver.get('http://web.whatsapp.com')
# Set saved cookies
cookies = {'name1': 'value1', 'name2', 'value2'}
for name in cookies:
driver.add_cookie({
'name': name,
'value': cookies[name],
})
# Load page using cookies
driver.get('http://web.whatsapp.com')
To get your cookies you can use the console (F12), Network tab, right click on the request, Copy => Copy Request Headers.
It should not be like that. It only opens the new window when initialized with new variable or the program starts again. Here is the code for chrome. It doesn't matter how many times you call driver.get(url) it would open the url in the same browser window
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(executable_path=r"C:\new\chromedriver.exe")
driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
time.sleep(10)
driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
time.sleep(10)
driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
time.sleep(10)
Let me know if the issue is resolved or you are trying to do something else.

Starting a browser in selenium

I want to run tests with selenium. IE gives me a modal error after bringing up IE 8 with this text "This is the initial start page for the WebDriver server" :
from selenium import webdriver
import time
browser = webdriver.Ie() # Get local session of IE
browser.get("http://www.google.com") # Load page
time.sleep(5)
browser.close()
So I tried Chrome.
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.google.com")
time.sleep(5)
browser.close()
and Selenium errors for not having the right path to the chrome.exe application. Chrome is installed as expected... C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe
A little help here would be greatly appreciated.
Have u downloaded the Chrome Driver?
To get set up, first download the appropriate prebuilt server. Make sure the server can be located on your PATH or specify its location via the webdriver.chrome.driver system property.
Then when u run
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.google.com")
time.sleep(5)
browser.close()
It should work.

Categories