简体   繁体   English

如何使用 Selenium Python 在网站上单击元素

[英]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.所以,我对 Python 和 HTML 不是很熟悉,但是我编写了这个代码,所以我可以在网站上的投票中投票,就我而言,这个想法在道德上没有任何问题,因为它鼓励刷新你的页面,这样您就可以一遍又一遍地为同一个人投票。 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.所以我已经访问了网站,但我不确定我要投票的人是否有 HTML ID/Class/Element,我不确定如何使用检查元素或如果它甚至有一个 HTML ID/类/元素。 If I want to vote for someone like Justin Herbert of the LAC, how should my code look?如果我想投票给像 LAC 的 Justin Herbert 这样的人,我的代码应该是什么样的? How would I click the white button next to Justin with Python?如何使用 Python 单击 Justin 旁边的白色按钮? Thank you!谢谢!

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.从 driver.get 我们得到一个弹出窗口,一个 iframe 来处理,我们需要点击响应播放器的 div 标签。 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:

<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

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

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