所以,我在一个有 8 个班级的谷歌教室里面,我想让 selenium 中间点击并在新选项卡中打开这些班级。
例如,这是我的一个班级,我让它通过 xpath 查找元素,我想让 selenium 通过单击鼠标中键在新选项卡中打开这个元素。我该怎么做?
Modern_history = driver.find_element_by_xpath('/html/body/div[4]/div/div[1]/div/ol/li[1]/div[1]/div[3]/h2/a[1]/ div[1]')
只需使用 javascript 打开新窗口:
element = driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/ol/li[1]/div[1]/div[3]/h2/a[1]")
driver.execute_script("window.open('arguments[0]');", element.get_attribute("href")
此外,您应该尝试在没有那么长的 xPath 的情况下查找元素,因为它非常脆弱。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句