【问题标题】:How to use selenium to click on this button? [duplicate]如何使用 selenium 点击这个按钮? [复制]
【发布时间】:2020-05-20 04:46:34
【问题描述】:

我正在尝试使用selenium 单击此webpage 上的展开按钮,即加号。

以下是该元素的页面来源:

<header>
<ol class="map">
    <li class="parent"><a href="../index.html" class="main">Random</a></li>
    <li class="parent"><a href="index.html" class="main">16. Brownian Motion</a></li>
    <li class="child"><a href="Standard.html" class="main" title="Standard Brownian Motion">1</a></li>
    <li class="current">2</li>
    <li class="child"><a href="Bridge.html" class="main" title="The Brownian Bridge">3</a></li>
    <li class="child"><a href="Geometric.html" class="main" title="Geometric Brownian Motion">4</a></li>
    <li class="details"><button type="button" title="Expand Details" onclick="expandDetails(true);"><img src="../icons/Plus.png" alt="Expand" /></button></li>
    <li class="details"><button type="button" title="Contract Details" onclick="expandDetails(false);"><img src="../icons/Minus.png" alt="Contract" /></button></li>
</ol>

我认为关键在于最后两行:

<li class="details"><button type="button" title="Expand Details" onclick="expandDetails(true);"><img src="../icons/Plus.png" alt="Expand" /></button></li>
<li class="details"><button type="button" title="Contract Details" onclick="expandDetails(false);"><img src="../icons/Minus.png" alt="Contract" /></button></li>

我阅读了4. Locating Elements,但找不到合适的方法。

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

您能否就这个问题向我解释一下?

【问题讨论】:

    标签: selenium


    【解决方案1】:

    虽然 x 路径下方不是那么好,但按钮上发生了点击。

    您没有共享任何代码来参考,请检查以下几行是否可以帮助您..

    它在 python 中,所以如果你正在使用其他语言,只需修改..

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver import ActionChains
    import time
    
    opt=webdriver.ChromeOptions()
    
    opt.add_argument("--start-maximized")
    
    driver= webdriver.Chrome(executable_path="C:\\chrome driver\\chromedriver.exe",options=opt)
    driver.get("http://www.randomservices.org/random/brown/Drift.html")
    
    # Wait for page to be loaded
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '/html/body/header/h2')))
    time.sleep(1) # just to ensure element is properly loaded
    
    plus_button = driver.find_element_by_xpath('/html/body/header/ol/li[7]/button')
    plus_button.click()
    # you can use Action chain as well for click
    time.sleep(4)
    minus_button  = driver.find_element_by_xpath('/html/body/header/ol/li[8]/button')
    ActionChains(driver).move_to_element(minus_button).pause(1).send_keys(Keys.ENTER).perform() 
    

    【讨论】:

    • 非常感谢您的热心帮助!这两行 plus_button = driver.find_element_by_xpath('/html/body/header/ol/li[7]/button')plus_button.click() 是键 :))
    • 当列表中有更多项目时它将不起作用,顺序(7、8)可能会改变。这是按标题查找元素的答案stackoverflow.com/questions/25363966/…
    • @LinhNguyen - 通过问题中的链接检查页面的 HTML,有 2 个元素具有相同的标题,我在您共享的链接中没有看到,所有 LI 都有相同的类和没有 ID 或 Name 属性,我已经提到这不是一个好的路径,因为页面构建是这样的。
    • @HietshKumar 您可以在 xpath 中组合 (className + title)
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 2014-10-12
    • 1970-01-01
    • 2019-02-23
    • 2019-01-02
    • 2023-03-10
    • 2020-06-24
    相关资源
    最近更新 更多