how to click a link in a new tab using selenium in python

Viewed 29

I'm a novice in programming. I've been learning about the selenium library recently, and now I have a problem about how to open a link in a new tab using css. This is the code I practice. My goal is to get into baidu(a engine to search), and search this character and I want the computer to open the first link, but it seems not work... I try to use that href, but it seems not working, probably the way how I deal have enter image description hereproblem

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
wd = webdriver.Chrome()

wd.implicitly_wait(10)

# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
wd.get('https://www.baidu.com')

element = wd.find_element(By.ID, 'kw')

element.send_keys('边城\n')


element = wd.find_element(By.CLASS_NAME, 'c-title t')
element.click()

I learned somethings like this, but I don't whether it useful or not overhere , this is the function of opening another tabs

for handle in wd.window_handles:

    wd.switch_to.window(handle)

    if '边城' in wd.title:

        break
0 Answers
Related