简体   繁体   中英

Click on link using selenium webdriver

I am trying to click on a link and can't seem to get it to work. I click all the way up to the page I need, but then it won't click the last link. The code is as follows:

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()

I receive the error message that the element is not clickable at point... and another element would receive the click. Any help would be greatly appreciated. I don't care which contest is clicked on as long as it is on the featured page which the previous code directs it too.

HTML代码 网页截图

There can be multiple reasons for that.

1. You might have to scroll down or might have to perform some action so that it'll be visible to script.

for scroll down you can use this code :

browser.execute_script("window.scrollTo(0, Y)")

where Y is the height (on a fullhd monitor it's 1080)

2. There can be multiple web element present , in this case you have to use unique element from dom , you can check that by right click > inspect > under element section > CTRL+F > write your locator (in your case xpath) to check how many entries are present.

You can try with this xpath also :

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

Code :

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()

Replace click event with action class, which will solve this Exception

from selenium.webdriver.common.action_chains import ActionChains

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

I was able to overcome this exception by using Click through JavaScript executor instead of regular Element.click(), as below-

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());

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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