简体   繁体   English

Python Selenium找不到元素标题(xpath)

[英]Python Selenium Can't find element title (xpath)

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()

I'm trying to find an element with the title as stated in my code. 我试图找到一个标题,如代码中所述。 It is basically selection the time slot for which the room is available but i cannot do it by this xpath. 基本上,这是选择房间可用的时隙,但是我不能通过此xpath进行选择。 I want to make it repeatable for different days and the ID changes everyday. 我想使其在不同的日子可以重复使用,并且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">&nbsp;</a>

This will work perfectly: 这将完美地工作:

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()

However, don't forget you will get an error if the element is not clickable (the room is not available). 但是,请不要忘记,如果元素不可单击(房间不可用),则会出现错误。 Keep that in mind! 记住这一点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM