在使用selenium和python进行webscraping期间无法识别唯一元素

时间:2017-06-27 04:38:27

标签: python selenium web-scraping

我正在尝试使用Selenium和Python来搜索[此站点] [1]。以下是社区帮助下的更新代码:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
url="http://www.marketsmojo.com/Stocks?StockId=170831&Exchange=0#navFinancialsAnnual"
browser=webdriver.Chrome()
browser.maximize_window()
browser.get(url)
time.sleep(3)
financials = browser.find_element_by_xpath("//div[@id='myNavbar']//a[contains(text(),'Financials')]")
actions = ActionChains(browser)
actions.move_to_element(financials)
actions.perform()
browser.find_element_by_xpath("//a[contains(text(),'Annual Results')]").click()
all_data = browser.find_element_by_xpath("//a[@id='PrevDetails']")
browser.execute_script("window.scrollTo(0, 250);")
all_data.click()

该元素仍然不可见,从错误Stack:

中可以看出
C:\Users\user\Desktop>test.py
Traceback (most recent call last):
  File "C:\Users\user\Desktop\test.py", line 13, in <module>
    browser.find_element_by_xpath("//a[contains(text(),'Annual Results')]").clic
k()
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\selenium\webdriver\remote\webelement.py", line 77, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\selenium\webdriver\remote\webelement.py", line 493, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response    
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

2 个答案:

答案 0 :(得分:0)

以下是您的问题的答案:

以下代码块将打开网址http://www.marketsmojo.com/Stocks?StockId=170831&Exchange=0#navDashboard,鼠标悬停在Financials上,点击Annual Results,然后点击All Data,即会显示Axtel Industries All Annual Results弹出:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

browser=webdriver.Chrome(r"C:\\Utility\\BrowserDrivers\\chromedriver.exe")
browser.maximize_window()
browser.get('http://www.marketsmojo.com/Stocks?StockId=170831&Exchange=0#navFinancialsAnnual')
browser.implicitly_wait(20)
financials = browser.find_element_by_xpath("//div[@id='myNavbar']//a[contains(text(),'Financials')]")
actions = ActionChains(browser)
actions.move_to_element(financials)
actions.perform()
browser.find_element_by_xpath("//a[contains(text(),'Annual Results')]").click()
all_data = browser.find_element_by_xpath("//a[@id='PrevDetails']")
browser.execute_script("window.scrollTo(0, 250);")
all_data.click()

如果这回答你的问题,请告诉我。

答案 1 :(得分:0)

将定位器从xpath更改为all_data到css“#navFinancialsAnnual #PrevDetails”并尝试。