使用selenium进行刮擦,无法获得单击按钮并使用我想要的数据加载隐藏表

时间:2018-03-22 23:02:18

标签: python selenium selenium-chromedriver

我正在尝试抓取一个包含导航标签的表单,该标签显示4种不同的方式来搜索所请求的信息。该URL加载到我要搜索的选项卡中,并自动填写日期字段。当人类用户想要查看信息时,他们只需要单击所选选项卡中的搜索按钮,即启用加载屏幕和填充,并在下面的表格中显示我想要抓取的数据。我目前正在使用python 3和selenium。

按钮代码:

<button type="submit" id="community-search" class="btn btn-primary" data-bind="click: submitSearch, enable: searchEnabled">Search</button>

这是我现在的代码:

#start of code after imports
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')

dir_path = os.path.dirname(os.path.realpath(__file__))
chromedriver = dir_path + "/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
browser = webdriver.Chrome(chrome_options = options, executable_path = chromedriver)

# LOGS INTO THE SITE 
browser.get('https://***.com/')
username = browser.find_element_by_name('UserName')
username.send_keys('***') 
password = browser.find_element_by_name('Password')
password.send_keys('***')
browser.find_element_by_css_selector('#sign-in').click()
# LOGIN WORKS

#GOES TO THE SCHEDUALS PAGE 
browser.get('https://***.com/Schedules')
#I KNOW IT IS NAVIGATING TO THIS PAGE

# I HAVE TRIED WAITING AND FOUND NO LUCK, TRYING TO CLICK THE BUTTON LEADS TO A HUGE LIST OF TRACEBACKS
browser.find_element_by_css_selector('community-search').click()
#WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.ID, 'community-search'))).click

#end of code

错误 -

    Traceback (most recent call last):
  File "Project.py", line 29, in <module>
    browser.find_element_by_css_selector('community-search').click()
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 589, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element
    'value': value})['value']
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"community-search"}

我是python和web抓取的新手,这是我的第一篇文章,所以我希望这是足够的信息。任何帮助将非常感谢

ImageOfSiteBeforeBtnPress ImageOfSiteAfterBtnPress

2 个答案:

答案 0 :(得分:1)

您的CSS选择器缺少#,因为community-search是一个ID。

所以而不是:

browser.find_element_by_css_selector('community-search').click()

应该是:

browser.find_element_by_css_selector('#community-search').click()

另一种方法是:

browser.find_element_by_id('community-search').click()

答案 1 :(得分:0)

错误说明了一切:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"community-search"}

错误清楚地表示 NoSuchElementException ,因为您使用的 css-selector 不能识别唯一元素:

browser.find_element_by_css_selector('community-search').click()

包含社区搜索id的值,正确css_selector将是(在值之前添加#),如下所示:< / p>

browser.find_element_by_css_selector("#community-search").click()

但是当您尝试在调用browser.get()后立即搜索 WebElement 时,您可能需要将WebDriverWaitexpected_conditions子句一起引导为{{ 3}}如下:

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#community-search"))).click()

如果您想使用id属性,可以使用以下代码行:

browser.find_element_by_id("community-search").click()

使用id属性以及 WebDriverWait 点击 WebElement

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "community-search"))).click()