2017-08-15 89 views
0

不確定爲什麼我收到以下錯誤。Python Selenium元素在彈出窗口中不可見

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible 

我試圖點擊文本框得分鏈接點擊「更多」按鈕,每場比賽(每場比賽不具有GT鏈接爲此事)之後。我有問題,因爲此按鈕顯示在彈出式窗口中?

我試圖通過使用time.sleep(5)左右來解決這種情況,但是同樣的錯誤打印到屏幕上,所以它似乎不是等待鏈接加載/出現的問題。

這裏是我的代碼:

schedule = 'http://www.gamecocksonline.com/sports/m-basebl/sched/scar-m-basebl-sched.html' 
options = webdriver.ChromeOptions(); options.add_argument("--start-maximized") 
driver = webdriver.Chrome(chrome_options=options) 
driver.get(schedule) 

#Find 'More' button, scroll to, and click 
python_button = driver.find_elements_by_xpath("//a[@class='sch-view-more']")[idx] 
driver.execute_script("return arguments[0].scrollIntoView();", python_button) 
driver.execute_script("window.scrollBy(0, -150);") 
python_button.click() 

#Click on Box Score link 
python_button = driver.find_element_by_xpath("//a[text()='Box Score']") 
python_button.click() 

#Pass HTML to Beautiful Soup and close browser window 
soup = BeautifulSoup(driver.page_source) 
playbyplay(soup) #Reads info from the html 
driver.quit() 
+0

當我使用chrome來「檢查元素」並複製xpath時,我得到的值與你的值不同。例如'// * [@ id =「2020541」]/td [6]/a','// * [@ id =「2020542」]/td [6]/a' ....等等「更多」元素 –

+0

僅供參考,我感興趣的第一個「更多」元素是對克萊姆森的第一場比賽(因爲它是沒有GT鏈接的第一場比賽)。可能是區別的原因 – rahlf23

+0

您嘗試在哪一行點擊「Box Score link」? – DebanjanB

回答

0

我能在這裏解決我自己的問題(感謝@DebanjanB在評論指出的幾件事情)。

增加等待時間以允許鏈接變爲可用,並且觀察頁面頂部的遊戲遵循與我正在搜索的那個相同的xpath是主要分辨率。

#Click on Box Score link 
time.sleep(1) 
python_button = driver.find_elements_by_xpath("//span[@class='sch-event-related']")[-1] 
python_button.click() 
相關問題