Selenium in python get the new tab page file(html) - python

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().

Related

Selenium get() redirects to another url

I'm trying to navigate to the following page and extract the html https://www.automobile.it/annunci?b=data&d=DESC, but everytime i call the get() method it looks like the website redirects me to the another page, always the same one which is https://www.automobile.it/torrenova?radius=100&b=data&d=DESC.
here's the simple code i'm running:
from selenium import webdriver
driver = webdriver.Chrome(executable_path=ex_path)
driver.get("https://www.automobile.it/annunci?b=data&d=DESC")
html=driver.page_source
if i do the same thing using the request module i don't get redirected
import requests
html=requests.get("https://www.automobile.it/annunci?b=data&d=DESC")
i don't understand why it's behaving like this, any ideas?
Use driver.delete_all_cookies()
from selenium import webdriver
driver = webdriver.Chrome(executable_path=ex_path)
driver.delete_all_cookies()
driver.get("https://www.automobile.it/annunci?b=data&d=DESC")
html=driver.page_source
PS: be also warned: Page_source will not get you the completed DOM as rendered.
Well you can clear browser cache by using the below code :
I am assuming that you are using chrome.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=ex_path)
driver.get('chrome://settings/clearBrowserData')
driver.find_element_by_xpath('//settings-ui').send_keys(Keys.ENTER)
driver.get("https://www.automobile.it/annunci?b=data&d=DESC")

How to open the another web page without changing the current page on Selenium Python?

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/')

Opening new tab from string url

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 +"');")

Selenium Python - Getting the current URL of web browser?

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

selenium can't submit form

I'm new to selenium and trying to automate the download of some government data. When using the code below. I manage to navigate to the right page and enter the right parmeter in the form, but then can't find a way to click the 'submit' button. I've tried find_element_by_partial_link_text("Subm").click() and I've tried find_element_by_class_name on a number of class names. Nothing works. Any ideas?
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
main_url="http://data.stats.gov.cn/english/easyquery.htm?cn=E0101"
driver = webdriver.Firefox()
driver.get(main_url)
time.sleep(8)
driver.find_element_by_partial_link_text("Industry").click()
time.sleep(8)
driver.find_element_by_partial_link_text("Main Economic Indicat").click()
time.sleep(8)
driver.find_element_by_id("mySelect_sj").click()
time.sleep(3)
driver.find_element_by_class_name("dtText").send_keys("last72")
time.sleep(4)
try:
driver.find_element_by_class_name("dtFoot").click()
except:
driver.find_element_by_class_name("dtFoot").submit()
Solved my own problem, the key was using
driver.find_element_by_class_name(`dtTextBtn`)
instead of
driver.find_element_by_class_name(`dtTextBtn f10`)
The latter was what I saw in the source code, but the f10 blocked selenium.

Categories