Sldume
Sldume

Reputation: 1

How to click Elements on websites with Selenium Python

So, I'm not very familiar with Python nor HTML But I made this code so I could vote in a poll on a website, as far as I'm concerned there is nothing morally wrong with this idea because it is encouraged to refresh your page so you can vote for the same person over and over again. I want to use the code to click a button. Below is the code I've got so far

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
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.nfl.com/voting/rookies/")

So I've gotten as far as going onto the website but I'm not sure if the person I am going to vote for has an HTML ID/Class/Element, I'm not really sure how to find it with inspect element or if it even has an HTML ID/Class/Element. If I want to vote for someone like Justin Herbert of the LAC, how should my code look? How would I click the white button next to Justin with Python? Thank you!

Upvotes: 0

Views: 138

Answers (1)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

From the driver.get we get a popup, an iframe to handle and we need to click on the div tag that responds to a player. You should be able to translate these steps to your player.

Popup:

<div class="slidedown-footer" id="slidedown-footer"><button class="align-right primary slidedown-button" id="onesignal-slidedown-allow-button">Allow</button><button class="align-right secondary slidedown-button" id="onesignal-slidedown-cancel-button">No Thanks</button><div class="clearfix"></div></div>

Iframe:

<iframe title="Content from Riddle" style="border: none; width: 100%; position: static; opacity: 1; height: 539px !important;" src="https://www.riddle.com/a/288390"></iframe>

Player:

<span class="list-title ng-binding" ng-bind-html="item.title">Salvon Ahmed, MIA</span>

For this we use the following:

driver.get("https://www.nfl.com/voting/rookies/")
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#onesignal-slidedown-cancel-button"))).click()    
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@title='Content from Riddle']")))  
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Salvon Ahmed, MIA']/parent::div/parent::div/div[2]/div"))).click()

Import

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

Upvotes: 1

Related Questions