尝试自动按下按钮

2024-05-08 14:26:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我认为这是一个相当简单的问题,但我不能为我的生活弄清楚。我正试图进入NBA球员表现道具页面:

https://www.bet365.com/#/AC/B18/C20559797/D47/E1/F43/P^47/Q^1/I

需要加载两次(首先显示奇怪的广告启动屏幕)。你知道吗

所以我让我的代码载入主页(www.bet365.com),然后选择左边的basketball,然后我试图找到页面上的“球员表现”按钮。这就是我的困难所在。我一直在尝试使用css选择器或xpath自动单击它。不管怎样,我尝试的任何东西都是无法吸引的。 enter image description here

这是我的代码到目前为止,是否有人有任何建议,以编程方式导航到NBA球员的表现页面。页面上的url每天都在变化,因此我需要按代码查找:

url = "https://www.bet365.com/"
driver = webdriver.Chrome('./chromedriver.exe ')
driver.get(url);
print("Waiting To Ensure Data Load 1 of 2")
time.sleep(5) # Let the user actually see something!
driver.get(url);
print("Waiting To Ensure Data Load 2 of 2")
time.sleep(5)
print("Naviagating to BasketballSection Section")
driver.find_element_by_css_selector("body > div:nth-child(1) > div > div.wc-PageView > div.wc-PageView_Main.wc-HomePage_PageViewMain > div > div.wc-HomePage_ClassificationWrapper.wc-CommonElementStyle_WebNav > div > div > div:nth-child(7)").click();
time.sleep(5)
urltwo = "body > div:nth-child(1) > div > div.wc-PageView > div.wc-PageView_Main > div > div.wc-CommonElementStyle_PrematchCenter.wc-SplashPage_CenterColumn > div.sm-SplashModule > div:nth-child(1) > div:nth-child(3) > div.sm-MarketGroup_Open > div > div.sm-MarketContainer.sm-MarketContainer_NumColumns1.sm-Market_Open > div > div"
submit = driver.find_element_by_css_selector(urltwo)
submit.click()

不知道该怎么做,我可以轻松地导航到左边的篮球栏。基本上我的目标是能够看到该页面,并解析找到链接到NBA球员的表现链接; 提前谢谢。你知道吗


Tags: 代码divcomchildurlwwwdriver页面
1条回答
网友
1楼 · 发布于 2024-05-08 14:26:15

对我来说,这段代码在Windows 10的Chrome中工作:

from selenium.webdriver.chrome.options import Options
from selenium import webdriver

options = Options()
options.add_argument(' disable-infobars')
driver = webdriver.Chrome(chrome_options=options)
url = "https://www.bet365.com/#/AC/B18/C20559797/D47/E1/F43/P^47/Q^1/I"
# First get only navigates to https://www.bet365.com
driver.get(url);
# Second one gets you to the full URL path.
driver.get(url);

如果确实需要遍历屏幕,请尝试location_once_scrolled_into_view将元素滚动到视图中。此代码也适用于我(与上述导入相同,但有以下附加功能):

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://www.bet365.com/#/AS/B18/"
driver.get(url);
driver.get(url);
player_perf_xpath = "/html/body/div[1]/div/div[2]/div[1]/div/div[2]/div[2]/div[1]/div[3]/div[2]/div/div[2]/div/div"
element = driver.find_element_by_xpath(player_perf_xpath)
# Scroll the screen so the element is visible and can be clicked
element.location_once_scrolled_into_view
element.click()

相关问题 更多 >