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)
Related
When I run this code, the page opens and closes. I can't reach the page. Everything is in the latest version. I am a Windows user.
I was trying to open Instagram page with this code.
enter image description here
I tried to open the instagram site with this code and the site was closed as soon as it was opened.
Im not familiar with selenium but I know for a fact that
get() just gets the HTML code of the website and does not display it
You have to add the below Chrome Option:
options.add_experimental_option("detach", True)
Full code:
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(chrom_driver_pat), options=options)
driver.get(<url>)
Next time, don't post the image, post the code and explain your issue clearly.
First of all, please, do not use images when your want a code review or help, it is more easier to help with raw code.
Selenium now uses Service method to run webdriver.
To run what you want, you need to fix some parts of your code, like this:
from selenium import webdriver
# in my case, i'm using selenium==4.5.0, for this version, is necessary to
# use selenium.webdriver.chrome.service to use driver path
from selenium.webdriver.chrome.service import Service as ChromeService
# For Windows path, use r"C:\..." when you will use \
# Remember to set the executable file too
chrome_driver_path = r"C:\pythondriver\chromedriver\chromedriver.exe"
# you can use "universal" path too, like linux, using / instead of \
# chrome_driver_path = "C:/pythondriver/chromedriver/chromedriver.exe"
# instead using:
# webdriver.Chrome()
# driver = webdriver.Chrome(chrome_driver_path)
# use this:
chrome_service = ChromeService(chrome_driver_path)
driver = webdriver.Chrome(service=chrome_service)
url = "https://instagram.com"
driver.get(url)
I tested using the configurations below and worked for me:
Chromedriver - 108.0.5359.71
Chrome - 108.0.5359.125 - 64 bits
If you need more help using Chrome Service, try look the Selenium Docs
When i run my program with the following code, it opens a new tab, that uses something like a guest account. Also, I am not able to log into anything with this tab.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(r'user-data-dir=C:\Users\Nick\AppData\Local\Google\Chrome\User Data\Profile 1')
driver = webdriver.Chrome(executable_path=r'C:\ChromeDriver\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.com/")
Same happened to me. The solution was to remove "\Profile 1" from the first line and add a new line for that.
options.add_argument(r'user-data-dir=C:\Users\Nick\AppData\Local\Google\Chrome\User Data')
options.add_argument('profile-directory=Profile 1')
this should work. If not the case, please check these answers: First and Second
I would like to open multiple chrome windows. Once they open, however, they close at the end of the for loop. can anyone help me? thank you so much
for i in range(numeroTask):
i = webdriver.Chrome(PATH)
i.get("https://www.youtube.com/")
This is how you can do it. I'm using window.open() to open a new tab and then driver.switch_to.window to switch to it, so you can open a url.
from selenium import webdriver
driver = webdriver.Chrome()
windows_count = 3
for i in range(windows_count):
# Opens a new tab
driver.execute_script("window.open()")
# Switch to the newly opened tab
driver.switch_to.window(driver.window_handles[i])
# Navigate to new URL in new window
driver.get("https://youtube.com")
# Close all tabs:
driver.quit()
Hopefully this helps, good luck!
Updated, way to do it with multiple chrome windows:
from selenium import webdriver
driver = webdriver.Chrome()
windows_count = 3
for i in range(windows_count):
# Opens a new tab
driver.execute_script('window.open("https://youtube.com", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=800, height=900, top=10, left=10");')
# Close all windows:
driver.quit()
DO you want to open simultaneously? Then you should try threads, async functions.
In the following two blocks of code, the last line seems to execute differently and I'm not sure why:
from selenium import webdriver
import time
profile = webdriver.FirefoxProfile('Path to firefox profile')
profile.set_preference("dom.webdriver.enabled", False)
profile.update_preferences()
driver = webdriver.Firefox(profile)
driver.get("https://google.com/")
time.sleep(6)
driver.execute_script("window.open('');")
driver = webdriver.Firefox()
driver.get("https://google.com/")
time.sleep(6)
driver.execute_script("window.open('');")
In the first snippet, driver.execute_script opens up a new window, but in the second snippet,driver.execute_script opens up a new tab. Why do the two snippets have different behavior for driver.execute_script ?
My guess is that in the first snippet there is some profile preference that is causing it to create new windows instead of tabs but I'm not sure what profile setting to change to make the behavior match exactly.
Your second instance of browser is being opened because of driver = webdriver.Firefox(). driver.execute_script("window.open('');") will open new tab only.
I've made this little python script to automate opening the websites I need in the morning, take a look `
Required Modules
import webbrowser
Open the websites
`webbrowser.get('firefox').open_new_tab('https://www.netflix.com')
webbrowser.get('firefox').open_new_tab('https://www.facebook.com')
webbrowser.get('firefox').open_new_tab('https://www.udemy.com') `
And I don't know how to wait until the webpage is loaded before opening the next one (in another tab), any help?
You could take the approach as mentioned at How to wait for the page to fully load using webbrowser method? and check for a certain element in the page manually.
Another options would be to import time and call it after opening each tab time.sleep(5) which waits for 5 seconds before running the next line of code.
import webbrowser
from time import sleep
links = ['https://www.netflix.com', 'https://www.facebook.com', 'https://www.udemy.com']
for link in links:
webbrowser.get('firefox').open_new_tab(link)
sleep(5)
Selenium Implementation:
Note: This implemenetation opens your URL's in multiple windows rather than a single window and multiple tabs.
I will be using the chrome driver which you can install at https://chromedriver.chromium.org/downloads
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True) #this is just to keep the windows open even after the script is done running.
urls = ['https://www.netflix.com', 'https://www.facebook.com', 'https://www.udemy.com']
def open_url(url):
driver = webdriver.Chrome(executable_path=os.path.abspath('chromedriver'), chrome_options=chrome_options)
# I've assumed the chromedriver is installed in the same directory as the script. If not, mention the path to the chromedriver executable here.
driver.get(url)
for url in urls:
open_url(url)