我正在尝试使用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
您能帮我阐明一下这个问题吗?
答案 0 :(得分:0)
虽然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()