selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见

时间:2017-09-13 08:11:23

标签: python selenium

嗨我有硒中的python问题

首先我有这个代码

#load webdriver function from selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
from bs4 import BeautifulSoup, Comment
import pandas as pd


#Setting up Chrome webdriver Options
#chrome_options = webdriver.ChromeOptions()

#setting  up local path of chrome binary file
#chrome_options.binary_location = 
"/Users/Norefly/chromedriver2/chromedriver.exec"

#creating Chrome webdriver instance with the set chrome_options
driver = webdriver.Chrome("/Users/Norefly/chromedriver2/chromedriver")
link = "https://play.google.com/store/apps/details?
id=com.supercell.clashofclans&hl=en"
driver.get(link)
Ptitle = driver.find_element_by_class_name('id-app-
title').text.replace(' ','')
print(Ptitle)
sleep(1)
driver.find_element_by_xpath
('//*[@id="body-
content"]/div/div/div[1]/div[2]/div[2]/div[1]/div[4]
/button[2]/div[2]/div/div').click()
sleep(1)
#driver.find_element_by_css_selector('.review-filter.id-review-sort-
filter.dropdown-menu-container').click()
driver.find_element_by_css_selector('.displayed-child').click()
#driver.find_element_by_xpath("//button[@data-dropdown-
value='1']").click()
driver.execute_script("document.querySelectorAll('button.dropdown-
child')[0].click()")
reviews_df = []



for i in range(1,10):
    try:
        for elem in driver.find_elements_by_class_name
('single-review'):
            print(str(i))
            content = elem.get_attribute('outerHTML')
            soup = BeautifulSoup(content, "html.parser")
            #print(soup.prettify())
            date = soup.find('span',class_='review-date').get_text()
            rating = soup.find('div',class_='tiny-star')['aria-label']
[6:7]
            title = soup.find('span',class_='review-title').get_text()
            txt = soup.find('div',class_='review-
body').get_text().replace('Full Review','')[len(title)+1:]
            print(soup.get_text())
            temp = pd.DataFrame({'Date':date,'Rating':rating,'Review 
Title':title,'Review Text':txt},index=[0])
            print('-'*10)
            reviews_df.append(temp)
    except:
        print('s')
    driver.find_element_by_xpath('//*[@id="body-
    content"]/div/div/div[1]/div[2]/div[2]/div[1]/div[4]/button[2]
    /div[2]/div/div').click()


    reviews_df = pd.concat(reviews_df,ignore_index=True)

    reviews_df.to_csv(Ptitle+'_coc1.csv', encoding='utf-8')

driver.close()   

我有这个错误

Traceback (most recent call last):
File "reviewex.py", line 54, in <module>
    driver.find_element_by_xpath('//*[@id="body-
 content"]/div/div/div[1]/div[2]/div[2]/div[1]
/div[4]/button[2]/div[2]/div/div').click()
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/selenium/webdriver/remote/webelement.py", line 78, in click
self._execute(Command.CLICK_ELEMENT)
File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/selenium/webdriver/remote/webelement.py", line 499, in 
_execute
return self._parent.execute(command, params)
File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
self.error_handler.check_response(response)
File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/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
(Session info: chrome=60.0.3112.113)
(Driver info: chromedriver=2.32.498537 
(cb2f855cbc7b82e20387eaf9a43f6b99b6105061),platform=Mac OS X 10.12.6 
x86_64)

我有这个问题... selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见 我知道下一页的审核时间是加载时间短 所以我尝试了“webdriverwait&#39;和&#39; times.leep&#39;,但它不起作用。 为什么这个问题...请帮助我...

1 个答案:

答案 0 :(得分:0)

是硒的comman问题, 尚未加载项目,您想要访问该项目。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

 elm = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'yourelement id ')))

您可以在此处获取更多详细信息 http://selenium-python.readthedocs.io/waits.html

另一种方式是明确等待,

在出现错误的行之前编写这些代码

import time
time.sleep(10)