简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM