简体   繁体   English

如何通过使用 python selenium 单击链接按钮访问在新选项卡中打开的元素?

[英]How to access an element that open in new tab via clicking a link button using python selenium?

There is a button in the website.网站上有一个按钮。 When I click by click() method, it opens in the new tab.当我单击 click() 方法时,它会在新选项卡中打开。

I tried this method to access the element which is located in the second tab.我尝试了这种方法来访问位于第二个选项卡中的元素。

for _ in range(3):
    time.sleep(5)
    driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

    try:
        content = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "/html/body/div/p[1]"))).text
        print(content)
    except:
        print("...")

and the result was,结果是,

...
...
...

I guess, it accepts the first tab as an active tab.我猜,它接受第一个选项卡作为活动选项卡。 How to access an element in the other tab, by using python selenium.如何使用 python selenium 访问另一个选项卡中的元素。 I searched some solution, and they were saying almost same thing which is to use CTRL+TAB method same as mine...我搜索了一些解决方案,他们说的几乎是一样的东西,那就是使用与我相同的 CTRL+TAB 方法......

You need to switch your driver to the new tab, then switch it back when it's done.您需要将驱动程序切换到新选项卡,然后在完成后将其切换回来。

Assuming you have 2 tabs:假设您有 2 个标签:

# you already opened new tab
tab1 = driver.window_handles[0]
tab2 = driver.window_handles[1]

driver.switch_to.window(tab2) # switch to new tab

# do your stuff here

driver.close() # close new tab

driver.switch_to.window(tab1) # switch to original tab

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用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? 如何使用python和selenium在新选项卡中打开链接 - How to open a link in a new tab with python and selenium 在Python中使用Selenium WebDriver在新标签页/窗口中打开链接 - Using Selenium WebDriver in Python to Open Link in New Tab/Window 单击使用Selenium和python打开新标签页的链接时,无法专注于firefox中的新标签页 - Unable to focus on new tab in firefox when clicking on the link that opens a new tab using selenium and python 如何使用 Python selenium ChromeDriver 在新标签页中打开链接 - How to open a link in a new tab with Python selenium ChromeDriver 如何使用 python selenium 在新选项卡中打开链接而不知道其 URL - How to open a link without knowing its URL in new tab using python selenium python selenium 在新标签页中打开链接并继续 session 在新标签页中 - python selenium open link in new tab and continue session in new tab 在新选项卡中打开链接,而不是单击 Class 找到的元素名称| Python - Open link in new tab instead of clicking the element found by Class Name| Python 如何通过硒打开新网站,然后打开新标签页并关闭新标签页并使用python中的第一个标签页 - How to open new website via selenium and than open new tab and close new tab and work with first tab in python Selenium 选择一个元素并使用 python 单击按钮 - Selenium selecting an element and clicking the button using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM