Python 使用SeleniumWebDriver单击链接

Python 使用SeleniumWebDriver单击链接,python,selenium,webdriver,Python,Selenium,Webdriver,我试图点击一个链接,但似乎无法让它工作。我一直点击到我需要的页面,但是它不会点击最后一个链接。代码如下: from selenium import webdriver from selenium.webdriver.support.ui import Select from selenium.webdriver.common.keys import Keys import time from bs4 import BeautifulSoup import requests import pan

我试图点击一个链接,但似乎无法让它工作。我一直点击到我需要的页面,但是它不会点击最后一个链接。代码如下:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
import time
from bs4 import BeautifulSoup
import requests

import pandas as pd
import openpyxl
from password import DKpassword
#import SendKeys

beginningTime = time.time()
browser = webdriver.Chrome()
browser.get('https://www.draftkings.com/lobby')
browser.maximize_window()
time.sleep(5)
signinLink = browser.find_element_by_xpath("""//*[@id="react-mobile-home"]/section/section[2]/div[2]/div[3]/div/input""")
signinLink.click()
signinLink.send_keys("abcdefg")
signinLink.send_keys(Keys.TAB)
passwordLink = browser.find_element_by_xpath("""//*[@id="react-mobile-home"]/section/section[2]/div[2]/div[4]/div/input""")
passwordLink.send_keys(DKpassword)
passwordLink.send_keys(Keys.ENTER)
time.sleep(5)

if browser.current_url == "https://www.draftkings.com/account/sitelogin/false?returnurl=%2Flobby1":
    signin = browser.find_element_by_partial_link_text("SIGN IN")
    signin.click()
elif browser.current_url == "https://www.draftkings.com/lobby#/featured":

    mlbLink = browser.find_element_by_partial_link_text("MLB")
    mlbLink.click()
else:
    print("error")

time.sleep(5)
featuredGame = browser.find_element_by_class_name("GameSetTile_tag")
featuredGame.click()
time.sleep(5)
firstContest = browser.find_element_by_partial_link_text("Enter")
firstContest.click()
我收到错误消息,该元素在点处不可单击。。。另一个元素将接收单击。任何帮助都将不胜感激。我不在乎点击哪个比赛,只要它是在前面的代码指导它的特色页面上


用action类替换click事件,这将解决此异常

from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
actions.move_to_element(firstContest).click().perform()

这可能有多种原因

1。您可能需要向下滚动或执行某些操作,以便脚本可以看到它

要向下滚动,可以使用以下代码:

browser.execute_script("window.scrollTo(0, Y)")
其中Y是高度(在全高清显示器上为1080)

2.可以存在多个web元素,在这种情况下,您必须使用来自dom的唯一元素,您可以通过右键单击元素部分下的>检查>CTRL+F>编写定位器(在您的示例中为xpath)来检查有多少条目的存在

您还可以尝试使用此xpath:

//a[contains(text(),'Enter') and contains(@href,'/contest/draftteam') and contains(@class,'dk-btn-dark')]  
代码:

WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Enter') and contains(@href,'/contest/draftteam') and contains(@class,'dk-btn-dark')]"))

firstContest = browser.find_element_by_xpath("//a[contains(text(),'Enter') and contains(@href,'/contest/draftteam') and contains(@class,'dk-btn-dark')]")
firstContest.click()

通过使用Click-through JavaScript executor而不是常规元素,我能够克服这个异常-

WebElement element = webDriver.findElement(By.xpath(webElemXpath));
try {

    JavascriptExecutor ex = (JavascriptExecutor) webDriver;
    ex.executeScript("arguments[0].click();", element);
    logger.info(elementName was + " clicked");
} 
catch (NoSuchElementException | TimeoutException e) {
logger.error(e.getMessage());

}

我不知道这是否与他们在页面上有多个“回车”按钮有关,但我不知道如何告诉他们抓取页面上第一个可用的按钮。你可以使用“browser.find_element_by_partial_link_text(“Enter”)”而不是“browser.find_element_by_link_text”(“Enter”)”。希望这能对你有所帮助。我还在学习Python,但我应该能够自己找出错误!!我首先使用了向下滚动的方法,找到了另一个包含“Enter”的链接,所以它试图点击它,但它当然不是可视的,所以它抛出了一个错误,无法点击。一旦我意识到这一点,我就使用//a contains来查找/draftteam,然后它就完美地工作了。谢谢你的帮助@肖恩斯克利尔:没问题,伙计。