How can I open 5 urls simultaneously on a single browser?
In addition, the script has to browse one by one through these 5 urls by performing the following tasks:
adding information on a field
select a CTA button
then click on a Send button
1 url must have a tab, so in total it should have 5 tabs running one by one.
here's my code but it didn't work
Thank you for your help
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.delete_all_cookies()
urls = ["https://business.google.com/u/0/edit/l/10199720925622488243?hl=fr",
"https://business.google.com/u/0/edit/l/13532588171385373346?hl=fr",
"https://business.google.com/edit/l/18307083220547614220",
"https://business.google.com/u/0/edit/l/08603059593698723407?hl=fr",
"https://business.google.com/edit/l/00810825496818981035"]
for posts in urls:
a = driver.execute_script("window.open('');")
driver.get(a)
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Chrome(executable_path=r"C:\Users\prave\Downloads\travelBA\chromedriver.exe")
driver.maximize_window()
driver.delete_all_cookies()
urls = ["https://business.google.com/u/0/edit/l/10199720925622488243?hl=fr",
"https://business.google.com/u/0/edit/l/13532588171385373346?hl=fr",
"https://business.google.com/edit/l/18307083220547614220",
"https://business.google.com/u/0/edit/l/08603059593698723407?hl=fr",
"https://business.google.com/edit/l/00810825496818981035"]
for posts in range(len(urls)):
print(posts)
driver.get(urls[posts])
if(posts!=len(urls)-1):
driver.execute_script("window.open('');")
chwd = driver.window_handles
driver.switch_to.window(chwd[-1])
// you can move to specific handle
chwd = driver.window_handles
print(chwd)
find the window handle and switch to it
Related
In selenium, I am grabbing some search result URL by XPATH. Now I want to click then one by one which will open then in the same browser one by one where the base URL is opened so that I can switch between then. How can I do that? I am giving my code below.
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
serv_obj = Service("F:\Softwares\Selenium WebDrivers\chromedriver.exe")
driver = webdriver.Chrome(service=serv_obj)
driver.maximize_window()
driver.implicitly_wait(5)
url = "https://testautomationpractice.blogspot.com/"
driver.get(url)
driver.find_element(By.XPATH, "//input[#id='Wikipedia1_wikipedia-search-input']").send_keys("selenium")
driver.find_element(By.XPATH, "//input[#type='submit']").click()
search_result = driver.find_elements(By.XPATH, "//div[#id='wikipedia-search-result-link']/a")
links = []
for item in search_result:
url_data = item.get_attribute("href")
links.append(url_data)
print(url_data)
print(len(links))
print(links)
I have grabbed all the links from the search result by using customized XPATH. I am being able yo print them also. But I want to open/click on the every resulted link one by one in the same browser.
You can do that as following:
Get the list of the links.
In a loop click on grabbed links.
When link is opened in a new tab switch the driver to the new opened tab.
Do there what you want to do (I simulated this by a simple delay of 1 second).
Close the new tab.
Switch back to the first tab.
Collect the list of links again since the previously collected links become Stale reference.
The following code works:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)
url = "https://testautomationpractice.blogspot.com/"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='Wikipedia1_wikipedia-search-input']"))).send_keys("selenium")
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#type='submit']"))).click()
links = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//div[#id='wikipedia-search-result-link']/a")))
for index, link in enumerate(links):
links[index].click()
driver.switch_to.window(driver.window_handles[1])
time.sleep(1)
driver.close()
driver.switch_to.window(driver.window_handles[0])
links = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//div[#id='wikipedia-search-result-link']/a")))
I want to send a string to the web page whose text field name is "inputfield". Actually, I can send the word to the page, but when I run the program, a new "chrome" page opens, which is used for testing purposes. However, I want to send a string to the field on a chrome page that is already open.
Here my code:
from selenium import webdriver
import time
url = "https://10fastfingers.com/typing-test/turkish"
options = webdriver.ChromeOptions()
options.binary_location = r"C://Program Files//Google//Chrome//Application//chrome.exe"
chrome_driver_binary = 'chromedriver.exe'
options.add_argument('headless')
driver = webdriver.Chrome(chrome_driver_binary, options=options)
driver.get(url)
driver.implicitly_wait(10)
text_area = driver.find_element_by_id('inputfield')
text_area.send_keys("Hello")
Nothing happens when I run this code. Can you please help? Can you run it by putting a sample web page in the url part?
Thank you.
EDIT: It is working when I deleted options. But still opening a new page when I run it. Is there a way use a page which already open on background.
chrome_driver_binary = 'chromedriver.exe'
driver = webdriver.Chrome(chrome_driver_binary)
driver.get('https://10fastfingers.com/typing-test/turkish')
text_area = driver.find_element_by_id('inputfield')
text_area.send_keys("Hello")
Click the popup prior to sending keys.
driver.get('https://10fastfingers.com/typing-test/turkish')
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "CybotCookiebotDialogBodyLevelButtonLevelOptinAllowallSelectionWrapper"))).click()
text_area = wait.until(EC.element_to_be_clickable((By.ID, "inputfield")))
text_area.send_keys("Hello")
Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I am not sure what is your question but if the issue is multiple tabs or windows being opened then:
you can switch between the windows as:
// you can move to specific handle
chwd = driver.window_handles
print(chwd)
driver.switch_to.window(chwd[-1])
you should shoul switch to the correct window before you can interact with elements on that window
just switch to the window that was already opened bypassing the index
If the problem is that you want to interact with an already opened chrome then you should follow below steps:
Start chrome with debug port:
<path>\chrome.exe" --remote-debugging-port=1559
Python :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1559")
driver = webdriver.Chrome(options=chrome_options)
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?
I've written below code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.google.co.jp/')
driver.get('https://www.yahoo.com/')
Of course this code just shows the 2nd page, but I want to show both of the pages at the same time. How do I implement it?
You need to use two separate webdriver.Chrome instances:
from selenium import webdriver
driver1 = webdriver.Chrome()
driver2 = webdriver.Chrome()
driver1.get('https://www.google.co.jp/')
driver2.get('https://www.yahoo.com/')
Using selenium and python. I am trying to get a URL and save it by doing this:
driver = webdriver.Firefox()
driver.get("https://google.com")
elem = driver.find_element(By.XPATH, "/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[1]/a")
elem.click()
url = driver.current_url
print url
url that prints is google.com and not the new clicked link which gmail.
My question is, how can I get the second url and save it.
You are getting the current url before the new page is loaded. Add an Explicit Wait to, for instance, wait for the page title to contain "Gmail":
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://google.com")
# click "Gmail" link
elem = driver.find_element_by_link_text("Gmail")
elem.click()
# wait for the page to load
wait = WebDriverWait(driver, 10)
wait.until(EC.title_contains("Gmail"))
url = driver.current_url
print(url)
Also note how I've improved the way to locate the Gmail link.