So I'm trying to open a new tab on Chrome where the URL is a string. I'm doing it this way because neither Action Chains or Keys seem to work. The purpose of this code is to open a new tab from a selected element but I can't seem to open a new page with the correct website.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
import random
chromedriver = "\Program Files\webdrivers/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("https://google.com")
time.sleep(3)
for a in driver.find_elements_by_xpath('//*[#id="prm"]/div/a'):
A = str(a.get_attribute('href'))
driver.execute_script("window.open('A');")
You are opening a new window with the URL of 'A'. It's being treated as a string because you aren't passing in the variable, just a letter. Try
driver.execute_script("window.open(arguments[0]);", A)
To open the hrefs in seperate TABs the correct syntax would be:
for a in driver.find_elements_by_xpath('//*[#id="prm"]/div/a'):
A = str(a.get_attribute('href'))
driver.execute_script("window.open('" + A +"');")
Related
I'm using selenium to check if a url is open in a tab, and if it is, print found. Here's my code:
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe')
driver.get("http://www.google.com/")
tabs_list = []
for handle in driver.window_handles:
driver.switch_to.window(handle)
tabs_list.append(driver.current_url)
url = 'https://www.google.com/'
for tab in tabs_list:
if url in tab:
print('found')
When I run this, nothing happens for several seconds, then a new Firefox window is opened, with an orange search bar. found also is not printed, even though google.com is running. When I print my tabs_list to see what is inside, I get ['about:blank'].
On further testing I discovered that it was searching for tabs in the new window. Is there any way to make it search through an existing window, instead of creating a new one?
Here's the solution, you're using url in quotes in other quotes, like:
driver.get('"http://www.google.com/"')
so that one should work perfectly:
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get("https://www.google.com/")
driver.quit()
I'm new at Python and Selenium. I'm trying to do something--which im sure im going in a very round-about way--any help is greatly appreciated.
The page im trying to parse through has different cards that need to be clicked on, i need to go to each card, and from there grab the name (h1) and the url. I havent gotten very far, and this is what i have so far.
I go through the first page, grab all the urls, add them to a list. Then i want to go through the list, and go to each url (opening a new tab) and from there grabbing the h1 and url. It doesn't seem like I'm even able to grab the h1, and it opens a new tab, then hangs, then opens the same tab.
Thank you in advance!
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get('https://zdb.pedaily.cn/enterprise//') #main URL
title_links = driver.find_elements_by_css_selector('ul.n4 a')
urls = [] #list of URLs
# main = driver.find_elements_by_id('enterprise-list')
for item in title_links:
urls.append(item.get_attribute('href'))
# print(urls)
for url in urls:
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get(url)
print(driver.find_element_by_css_selector('div.info h1'))
Well, there are a few issues here:
You should be much more specific with your tag for grabbing urls. This is leading to multiple copies of the same url--that's why it is opening the same pages again.
You should give the site enough time to load before trying to grab objects, that may be why it's timing out but always good to be on the safe side before grabbing objects.
You have to shift focus back to the original page to continue iterating the list
You don't need to inject JS to open a new tab and use a py call to open , and JS formatting could be cleaner
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome()
driver.get('https://zdb.pedaily.cn/enterprise/') # main URL
# Be much more specific or you'll get multiple returns of the same link
urls = driver.find_elements(By.TAG_NAME, 'ul.n4 li div.img a')
for url in urls:
# get href to print
print(url.get_attribute('href'))
# Inject JS to open new tab
driver.execute_script("window.open(arguments[0])", url)
# Switch focus to new tab
driver.switch_to.window(driver.window_handles[1])
# Make sure what we want has time to load and exists before trying to grab it
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.info h1')))
# Grab it and print it's contents
print(driver.find_element(By.CSS_SELECTOR, 'div.info h1').text)
# Uncomment the next line to do one tab at a time. Will reduce speed but not use so much ram.
#driver.close()
# Focus back on first window
driver.switch_to.window(driver.window_handles[0])
# Close window
driver.quit()
That is the code I use
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('http://www.youku.com')
inputElem = driver.find_element_by_id('headq')
inputElem.send_keys('星岚摄影社')
inputElem.send_Keys(Keys.RETURN)
print(driver.page_source)
The last print command printed (http://www.youku.com)'s page file, but that is not the file I want.
After the commandinputElem.send_Keys(Keys.RETURN) executed, Chrome will open a new tab. That is the page I want to get.
So the question is how can I get the page file I want.
Anyone can help me? thx in advance.
To alecxe
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('http://www.youku.com')
inputElem = driver.find_element_by_id('headq')
form = driver.find_element_by_id("qheader_search")
driver.execute_script("arguments[0].setAttribute('target', '_self');", form)
inputElem.send_keys('星岚摄影社')
inputElem.send_keys(Keys.RETURN)
print(driver.page_source)
One option would be to find the appropriate form element and reset the target attribute so that when you submit the form, it would happen in the same browser tab:
form = driver.find_element_by_id("qheader_search")
driver.execute_script("arguments[0].setAttribute('target', '_self');", form)
Also note the typo you have in the code: send_Keys() -> send_keys().
I have this so far:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('C:\Users\Fan\Desktop\chromedriver.exe')
url = driver.current_url
print url
It keeps saying that line 4 "driver" is an invalid syntax. How would I fix this?
Also is there a way I can get all the current tabs open, and not just a single one?
EDIT: the above code works now; But I have another problem!
The code now opens a new tab, and for some reason the URL bar has "data;" in it, and it outputs data; as the print.
But I want it to take the existing URL from existing web browser already opened, how do I solve this?
In Python you do not specify the type of variable as is required in Java which is the reason for the error. The same error will also happen because your last line starts with String.
Calling webdriver.Chrome() returns a driver object so the line webdriver driver = new webdriver() is actually not needed.
The new keyword is not used in Python to create a new object.
Try this:
from selenium import webdriver
driver = webdriver.Chrome()
url = driver.getCurrentUrl()
In order to extract the url of the current page from the web driver you have to call the current_url attribute:
from selenium import webdriver
import time
driver = webdriver.Chrome()
#Opens a known doi url
driver.get("https://doi.org/10.1002/rsa.1006")
#Gives the browser a few seconds to process the redirect
time.sleep(3)
#Retrieves the url after the redirect
#In this case https://onlinelibrary.wiley.com/doi/abs/10.1002/rsa.1006
url = driver.current_url
I am trying to send some shortcut keys to open a new tab in chrome, using selenium in python. I open up facebook, then I log in, then I want to open a new tab where I can pass the url of one of my friend so that I can view his profile. I wrote the following code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import getpass
driver = webdriver.Chrome()
#opening browser and loging into the account
def login():
driver.get("http://www.facebook.com")
elem = driver.find_element_by_xpath("//*[#id=\"email\"]")
elem.send_keys("myUsername")
password = driver.find_element_by_name("pass")
password.send_keys("myPassword")
elem.send_keys(Keys.RETURN)
driver.implicitly_wait(5)
def scout():
scroll = driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
driver.get("http://www.facebook.com/some.friend")
driver.implicitly_wait(8)
login()
scout()
But it gives me the error:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
I made the browser wait implicitly but still it's unable to find the tag. Please help.
First of all, try calling WebDriverWait instead of find_element_by_css_selector in order to find elements. It calls find_element_by_<method> every half a second until given timeout. That way you don't have to gamble with implicitly_wait or time.sleep().
Regarding your open new tab problem - you cannot send ctrl + t to chromedriver, it simply wouldn't do anything (it would work on firefox though). I've encountered this problem before, and the best solution I found is to search for a link in the website itself, so if you're on Facebook you can use that F icon at the top left. Then you can send ctrl + shift + click to open that in a new tab.
from selenium.webdriver import ActionChains
actions = ActionChains(driver);
actions.move_to_element(link)
actions.send_keys(Keys.CONTROL+ Keys.SHIFT)
actions.click(link)
actions.perform()
From there you should just switch tabs using
tabs = driver.window_handles
driver.switch_to.window(tabs[<index>])