[英]How to get link and page information of new opened tab in selenium python
i was trying to scrape this website.我试图抓取这个网站。 the process i followed was search a user click the first user and get information about him/her.
我遵循的过程是搜索用户单击第一个用户并获取有关他/她的信息。 after i clicked the user it is opening a new tab, so how can i get the information in the newly opened tab.
单击用户后,它正在打开一个新选项卡,那么我如何在新打开的选项卡中获取信息。
the code that i have written so far到目前为止我写的代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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('C:/Users/XXX/chromewebdriver/chromedriver.exe')
driver.get('https://www.tracksellers.com/')
search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='desktop-seller-search']")))
search.click()
word = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='d-seller-autocomplete']")))
word.send_keys('james')
searchdiv = word = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='d-seller-autocomplete-box']")))
searchresult = searchdiv.find_element_by_id('list')
searchresult = searchresult.find_elements_by_tag_name('li')
searchresult[0].click()
# the above code search's for a user and click on the first user
# next steps i tried
driver.window_handles
# gives me the below output
['CDwindow-70F83E4DCEB9D746B96FD9D965FC1BF7',
'CDwindow-87428C4E8AF3614CFE2C461A0B3AE765']
# trying to get the current tab by using this code, but it gives only the first tab
driver.current_window_handle
# output
'CDwindow-70F83E4DCEB9D746B96FD9D965FC1BF7'
How can i switch from one tab to another tab and access tags and other things.如何从一个选项卡切换到另一个选项卡并访问标签和其他内容。
It's in a new tab so you would need to switch the driver focus to the newly opened tab :它在一个新选项卡中,因此您需要将驱动程序焦点切换到新打开的选项卡:
driver.switch_to.window(driver.window_handles[1])
You need to write this code right after the click on first page.您需要在单击第一页后立即编写此代码。
maintab = driver.current_window_handle
for handle in driver.window_handles:
if handle != main_page:
othertab = handle
driver.switch_to.window(othertab)
After this you can use your code on tab2 Also ,with this code you can switch back to your main tab using在此之后,您可以在 tab2 上使用您的代码此外,您可以使用此代码切换回主选项卡
driver.switch_to.window(maintab)
again再次
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.