0

I'm trying to use selenium to click on the expand button on this webpage, which is the plus symbol.

enter image description here

Below is the page source of that elements:

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

I think the keys lie in the last 2 lines:

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

I read on 4. Locating Elements, but could not find an appropriate method.

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

Could you please shed me some lights on this issue?

Akira
  • 2,594
  • 3
  • 20
  • 45

1 Answers1

0

Though below x path is not that good but click is happening on the button.

you hv not shared any code to refer , check if below lines can help you..

its in python so just modify if you are working on some other language..

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() 
Hietsh Kumar
  • 1,197
  • 9
  • 17
  • Thank you so much for your dedicated help! These two lines `plus_button = driver.find_element_by_xpath('/html/body/header/ol/li[7]/button')` and `plus_button.click()` are keys :)) – Akira May 20 '20 at 05:23
  • it will not work when you have more items in the list, the order (7, 8) might change. Here is the answer to find elements by title https://stackoverflow.com/questions/25363966/find-and-click-element-by-title-python-selenium/ – Linh Nguyen May 20 '20 at 05:35
  • @LinhNguyen - Check the HTML of the page by link in the question , there are 2 elements which have same title i dont see that is covered in the link you shared ,, all LI has same Class and there is no ID or Name attribute,, i have already mentioned that this is not a good path as page built is like that. – Hietsh Kumar May 20 '20 at 06:53
  • @HietshKumar you can combine (className + title) in the xpath – Linh Nguyen May 20 '20 at 07:26