简体   繁体   English

在Python + Selenium中的新标签页中打开抓取的可点击按钮

[英]Open scraped clickable button in a new tab in Python + Selenium

I'm trying to, after scraping the link I want to follow, open it in a new tab. 我想在抓取要关注的链接后,在新标签页中打开它。 I've tried several ways but to no avail. 我尝试了几种方法,但无济于事。 My code: 我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

browser=webdriver.Chrome()
urlbet='https://www.bet365.es/?&cb=103265469#/HO/'

browser.get(urlbet)

sport=WebDriverWait(browser, 10).until(
        EC.presence_of_element_located((By.XPATH,'//*[@id="dv1"]/a')))

sport.click()

login = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, 'hm-Login')))

fields = login.find_elements_by_css_selector('.hm-Login_InputField')
button = login.find_element_by_css_selector('.hm-Login_LoginBtn')

user='my_user'
passw='my_pass'
fields[0].send_keys(user)
fields[1].click()
fields[2].send_keys(passw)
button.click()

With all that, I log in my account and arrive at my desired web page. 有了这些,我登录了我的帐户,然后转到所需的网页。

trial=WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.XPATH,'//div[contains(text(), "Baloncesto")]')))

And here, where I should open that button in a new tab, like right click "open on a new tab", I am not able. 在这里,我应该在新标签页中打开该按钮的位置,例如右键单击“在新标签页上打开”,我无法。 I've tried the following: 我尝试了以下方法:

trial.send_keys(Keys.COMMAND + 't')

and

trial.send_keys(Keys.CONTROL + Keys.SHIFT + Keys.RETURN)

both times what I get is: 两次我得到的是:

WebDriverException: unknown error: cannot focus element
(Session info: chrome=60.0.3112.113)
(Driver info: chromedriver=2.33.506120 
(e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64)

I'm running python on Windows 7, webdriver Google Chrome. 我在Windows 7,网络驱动程序Google Chrome上运行python。

Instead of trying with trial.send_keys(Keys.COMMAND + 't') and trial.send_keys(Keys.CONTROL + Keys.SHIFT + Keys.RETURN) try with action_chains as follows : 与其尝试使用trial.send_keys(Keys.COMMAND + 't')trial.send_keys(Keys.CONTROL + Keys.SHIFT + Keys.RETURN)action_chains尝试使用action_chains ,如下所示:

driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
action = ActionChains(driver)
elem = driver.find_element_by_link_text("Gmail")
action\
    .move_to_element(elem)\
    .key_down(Keys.SHIFT)\
    .click(elem)\
    .key_up(Keys.SHIFT)\
    .perform()

经过更多的分析后,我得出的结论是,该网站不提供右键单击选项,而是在新选项卡上打开,因此,即使CONTROL + SHIFT + Click应该起作用,实际上,它的操作就像是左键单击按钮,但这不是因为代码,而是因为Web对这种快捷方式做出了响应。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM