尝试自动按下按钮

时间:2017-12-01 21:18:47

标签: python selenium-webdriver webdriver

我认为这是一个非常简单的问题,但我无法理解我的生活。我正试图达到NBA球员表现道具页面:

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

需要加载两次(首先显示奇怪的广告启动画面)。

所以我的代码加载主页面(www.bet365.com),然后选择左侧的篮球,然后我尝试在页面上找到“玩家表现”按钮。这就是我遇到困难的地方。我一直试图通过使用css选择器或xpath自动点击它。但是我尝试的任何东西都是不可点击的。 enter image description here

到目前为止,这是我的代码,有没有人对如何以编程方式导航到NBA球员表现页面有任何建议。页面上的网址每天都会更改,因此我需要通过代码找到它:

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球员表演链接的链接; 提前致谢。

1 个答案:

答案 0 :(得分:1)

对我来说,此代码适用于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()