由于框架我找不到元素

时间:2019-06-10 10:05:51

标签: python selenium

我正在尝试设计一个自动预订系统。 因此,我需要找到一个元素:driver.find_element_by_link_text(“予约可”)。

如果元素在加载后可见,我可以找到该元素。 (在代码示例中,这意味着元素位于此范围的02:00-05:30) 所以我尝试找到框架,但失败了。我一直在寻找解决方案两天了。

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException 


driver = webdriver.Chrome()

url = 'https://eikaiwa.dmm.com/list/?data%5Btab1%5D%5Bstart_time%5D=02%3A00&data%5Btab1%5D%5Bend_time%5D=25%3A30&data%5Btab1%5D%5Bgender%5D=0&data%5Btab1%5D%5Bage%5D=%E5%B9%B4%E9%BD%A2&data%5Btab1%5D%5Bfree_word%5D=Kim+Khim&date=2019-06-13&tab=0&sort=6'

driver.get(url)   


try:
    button = driver.find_element_by_link_text("予約可")
    driver.execute_script("arguments[0].scrollIntoView(false);", button) # move to the position of the button    
except NoSuchElementException:
    print('not found any opening')

1 个答案:

答案 0 :(得分:1)

您想要的元素没有滚动,因此无法交互。

您可以通过以下方式找到元素:

button.location_once_scrolled_into_view

但是我找不到链接文本,而是找到了类名。

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException 

driver = webdriver.Chrome()

url = 'https://eikaiwa.dmm.com/list/?data%5Btab1%5D%5Bstart_time%5D=02%3A00&data%5Btab1%5D%5Bend_time%5D=25%3A30&data%5Btab1%5D%5Bgender%5D=0&data%5Btab1%5D%5Bage%5D=%E5%B9%B4%E9%BD%A2&data%5Btab1%5D%5Bfree_word%5D=Kim+Khim&date=2019-06-13&tab=0&sort=6'

driver.get(url)   

try:
    #button = driver.find_element_by_link_text("予約可")
    button = driver.find_element_by_class_name("bt-open")
    button.location_once_scrolled_into_view
    button.click()
except NoSuchElementException:
    print('not found any opening')