无法使用selenium单击父级

时间:2018-02-09 21:35:13

标签: python selenium

我需要使用点击此HTML:

<span title="Klik for at oprette booking" onclick="sende('proc_straks.asp','opret','11-02-2018;1;18;20:00;20:45;0;','','', '046009a83941f411fb90c9790a0c92a8')" style='height:52px;' class='banefelt btn_ledig link '>
<div class='padding5'>
<div>20:00 - 20:45</div>
<div class='clearfix'>
</div>
</div>
</span>

所以我尝试了这个代码:

for i in range(1500):
    if driver.find_elements_by_tag_name("div")[i].text == '20:00 - 20:45':    
        diven = driver.find_elements_by_tag_name("div")[i]
        diven.find_element_by_xpath('..').click()

我收到了这个错误:

selenium.common.exceptions.WebDriverException: Message: 
unknown error: Element <span title="Klik for at oprette booking" 
onclick="sende('proc_straks.asp','opret',
'10-02-2018;1;18;20:00;20:45;0;','','', 
'2e24985c6c1be987e1ab481b1588c28a')" style="height:52px;" 
class="banefelt btn_ledig link ">...</span> 
is not clickable at point (172, 611). 
Other element would receive the click: <p class="cc_message">...</p>e click:

2 个答案:

答案 0 :(得分:1)

我假设您要单击span元素,那么如何这样做:

span_list = driver.find_elements_by_tag_name("span")

for span in span_list:

    iterate_through_span_list = True

    div_list = span.find_elements_by_tag_name("div")

    for div in div_list:

        if div.text == '20:00 - 20:45':

            span.click()

            iterate_through_span_list = False

            break

    if iterate_through_span_list == False:

        break

答案 1 :(得分:0)

您使用间接方式查找所需的span,为什么不直接找到范围,但使用1到1500之间的外部for循环

spans = driver
    .find_elements_by_css_selector("span[title*='oprette booking']")

for span in spans:
  time_slot = span
    .find_element_by_css_selector("div.padding5 + div").text

  if time_slot == '20:00 - 20:45':
    span.click()
    break

not clickable知道chromedriver 2.31上的问题,当元素在当前视口中不可见时,如果您使用此版本,请升级到2.33或更高版本。

如果使用chromdriver版本,请检查span

是否包含任何内容