from selenium import webdriver
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
import collections
import datetime
browser = webdriver.Chrome()
browser.get('https://libcal.library.ucsb.edu/rooms.php?i=12405')
browser.find_element_by_link_text('18').click() #selects date
browser.find_element_by_xpath('//*[@title="2314, 12:00am to 12:30am, Thursday, October 18, 2018"]').click()
我正在尝试查找具有我的代码中所述标题的元素。基本上,这是选择房间可用的时隙,但是我不能通过此xpath进行选择。我想让它在不同的日子重复出现,并且ID每天都会更改。
<a href="#" class="lc_rm_a" data-seq="36825101" id="647749313"
onclick="return showBookingForm(this.id,'2314','12:00am - 12:30am,
Thursday, October 18, 2018', '30');" style="width: 30px; float:left;
display:block;" title="2314, 12:00am to 12:30am, Thursday, October 18,
2018"> </a>
答案 0 :(得分:0)
这将完美工作:
from selenium import webdriver
browser = webdriver.Chrome()
browser.implicitly_wait(2)
browser.get('https://libcal.library.ucsb.edu/rooms.php?i=12405')
browser.find_element_by_link_text('17').click()
code = '2574'
time = '1:00am to 1:30am'
date = browser.find_element_by_xpath('//*[@id="s-lc-rm-tg-h"]').text
browser.find_element_by_xpath('//*[@title="' + code + ', ' + time + ', ' + date + '"]' ).click()
browser.close()
但是,请不要忘记,如果元素不可单击(房间不可用),则会出现错误。记住这一点!