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