不能使用python

时间:2017-01-12 23:56:34

标签: python selenium selenium-webdriver selenium-chromedriver

使用硒和铬驱动器。 Python 3.5

想象一下开始时间和结束时间框。单击开始时间框,出现一个时钟。开始时钟左对齐,结束时钟右对齐。

<div class="popover clockpicker-popover top clockpicker-align-left" style="display: block; top: 511px; left: 287px;">

enter image description here enter image description here

<div class="clockpicker-dial clockpicker-hours" style="visibility: visible;"></div>

<div class="clockpicker-dial clockpicker-minutes" style="visibility: visible;"></div>

然后点击进入结束时间框:

enter image description here enter image description here

<div class="popover clockpicker-popover top clockpicker-align-right" style="display: block; top: 511px; left: 287px;">

相同的时间和分钟。

我的问题是选择时钟的左右版本。事实上,我的代码工作正常,直到我不得不放入结束时间,所以我决定添加代码来区分它们。

import time, sys
from selenium import webdriver

# open chrome and go to website
driver = webdriver.Chrome()
driver.get('website')
time.sleep(3)


def startHour(number):
    # If I remove this line I can select hours/mins for first clock but not second clock
    driver.find_element_by_css_selector('popover.clockpicker-popover.top.clockpicker-align-left')
    select = driver.find_element_by_css_selector('.clockpicker-dial.clockpicker-hours')
    print('Selected clockpicker hours')
    for hour in select.find_elements_by_class_name('clockpicker-tick'):
        print(hour.text)
        if hour.text == number:
            print('Hour.text: ' + hour.text)
            print('Hour: ' + number)
            hour.click()
            break

def endHour(number):
    driver.find_element_by_css_selector('popover.clockpicker-popover.top.clockpicker-align-right')
    select = driver.find_element_by_css_selector('.clockpicker-dial.clockpicker-hours')
    print('Selected clockpicker hours')
    for hour in select.find_elements_by_class_name('clockpicker-tick'):
        print(hour.text)
        if hour.text == number:
            print('Hour.text: ' + hour.text)
            print('Hour: ' + number)
            hour.click()
            break

def clickMinutes(number):
    select = driver.find_element_by_css_selector('.clockpicker-dial.clockpicker-minutes')
    print('Selected clockpicker minutes')
    for minutes in select.find_elements_by_class_name('clockpicker-tick'):
        print(minutes.text)
        if minutes.text == number:
            print('Minutes.text: ' + minutes.text)
            print('Minutes: ' + number)
            minutes.click()
            break

# Select start time input box, start time clock appears
driver.find_element_by_id('StartTime').click()

startHour('7')
time.sleep(4)

clickMinutes('30')
time.sleep(4)

# Select end time input box, end time clock appears
driver.find_element_by_id('EndTime').click()
time.sleep(4)

endHour('16')
time.sleep(4)

clickMinutes('00')
time.sleep(4)

driver.quit()
sys.exit()

错误:

  

selenium.common.exceptions.NoSuchElementException:消息:没有这样的   element:无法定位元素:{&#34;方法&#34;:&#34; css   选择&#34;&#34;选择器&#34;:&#34; popover.clockpicker-popover.top.clockpicker对齐左&#34;}

当我删除driver.find_element_by_css_selector('popover.clockpicker-popover.top.clockpicker-align-left')时,它对第一个时钟工作正常,我可以通过点击它的输入框打开第二个时钟,但我实际上无法选择第二个时钟或其元素。

更新

它似乎找到了时间,但它打印出一堆空行,这让我觉得它还在第二个时钟出现时隐藏的第一个时钟。

enter image description here

当我将我的打印语句从print(hour.text)更改为print(hour)时,它会显示:

enter image description here

24小时24元素。

解答:

我在popover.clockpicker...前面添加了句点,并且还通过css选择器附加了下一个查找元素,并且它有效!谢谢大家的帮助。

def startHour(number):
    select = driver.find_element_by_css_selector('.popover.clockpicker-popover.top.clockpicker-align-left').find_element_by_css_selector('.clockpicker-dial.clockpicker-hours')
    print('Selected clockpicker hours')
    for hour in select.find_elements_by_class_name('clockpicker-tick'):
        print(hour.text)
        if hour.text == number:
            print('Hour.text: ' + hour.text)
            print('Hour: ' + number)
            hour.click()
            break

def endHour(number):
    select = driver.find_element_by_css_selector('.popover.clockpicker-popover.top.clockpicker-align-right').find_element_by_css_selector('.clockpicker-dial.clockpicker-hours')
    print('Selected clockpicker hours')
    for hour in select.find_elements_by_class_name('clockpicker-tick'):
        print(hour.text)
        if hour.text == number:
            print('Hour.text: ' + hour.text)
            print('Hour: ' + number)
            hour.click()
            break

2 个答案:

答案 0 :(得分:1)

我猜您的问题是由选择器中的拼写引起的:

'popover.clockpicker-popover.top.clockpicker-align-left'

实际上意味着您要处理

 <popover class="clockpicker-popover top clockpicker-align-left">

元素,因此您需要将引导点添加为

'.popover.clockpicker-popover.top.clockpicker-align-left'

答案 1 :(得分:0)

所以,听起来它弹出第一个时钟(align-left),但是你的错误表明这个元素是失败的。那似乎不对......

另外,你提到在第二个时钟出现之前你必须实际点击第一个时钟,我不知道你在做什么。