简体   繁体   English

如何在selenium python中获取新打开的标签的链接和页面信息

[英]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.

相关问题 如何使用python和selenium在新选项卡中打开链接 - How to open a link in a new tab with python and selenium 如何通过在 Selenium Python 中运行的第一个网站获得新打开选项卡的 url - How can one get the url of a new opened tab by the first website that was running in Selenium Python 如何使用Selenium和Python在新选项卡中打开新链接(单击网页中的元素后生成)? - How to open the new link (generated after clicking an element in a web page) in a new tab using Selenium and Python? 如何下载使用 Selenium 在新选项卡中打开的文件 - How to download a file opened in a new tab with Selenium 如何使用 Python selenium ChromeDriver 在新标签页中打开链接 - How to open a link in a new tab with Python selenium ChromeDriver python中的Selenium获取新的标签页文件(html) - Selenium in python get the new tab page file(html) Python Selenium Chromedriver-点击后无法获取新打开标签的current_url() - Python Selenium Chromedriver - Can't Get current_url of new opened tab after click() 使用 Python Selenium 如何关闭单击网页上的合法链接时打开的当前选项卡 - Using Python Selenium how to close the current tab which got opened when clicked a legal link on webpage python selenium 在新标签页中打开链接并继续 session 在新标签页中 - python selenium open link in new tab and continue session in new tab 如何在Selenium for Python上由.click()打开的新选项卡上使用driver.current_url - How to use driver.current_url on a new tab opened by .click() on Selenium for Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM