1

I am trying to expand a list with Selenium WebDriver for Python, which is controlled by a button that looks like this:

List When Closed

To give you an idea of how it looks when open:

List When Open

The HTML looks like this:

<div>  ##just for more information on the tree
<div>  ##
<ul>   ##

<li>
role = "presentation"
ng-if = "session.hasOccurrences" ## this is the name of a span
class = "expand-children ng-scope"

<button
role = "button"
class = "icon-button has tooltip" 
bb-translate-attrs = "{aria-label ': 'session.session-list.toggle-occurrences-aria'}"
aria-pressed = "false"
ng-click = "expand()"
aria-label = "Toggle session occurrences">
</button>

</li>

</ul>  ##
</div> ##
</div> ##

Since it had a tag name "button", I tried to use the following but it did not work:

url = 'Example.com'
driver = webdriver.Chrome()
driver.get(url)

driver.find_element_by_tag_name("button").click()

I tried to use xpath and CSS selectors, which I have very minimal experience with. Is there something I could do to expand the list?

Thank you for your help in advance.

UPDATE 6/6/2020

My life became difficult because there was JS on the web page I am trying to access. One of the changes this JS makes is that it hides some of the elements' paths. It is strange; on the web page they are accessible and clickable, but no XPATH can locate them.

I tried to outsmart this by locating an accessible element on the page using ActionChains, then tabbing into the element I want to access and click it. But when I reach the element by tabbing, I can't click it. I printed the element to see what Python sees, and it sees "None". Is there any workaround to this?

Azazil
  • 11
  • 3
  • Maybe using action chains will do the job. check this out: [button clicks in selenium](https://stackoverflow.com/questions/21350605/python-selenium-click-on-button) – Shahid Khan Jun 02 '20 at 09:33
  • Thank you Mr. Khan. I looked into it carefully and used ActionChains. It looks like a promising tool. It did not raise any exceptions but the button was still not clicked. I might combine ActionChains with xpath and see what happens. Thank you for your answer very kindly. – Azazil Jun 02 '20 at 10:43
  • @Azazil Instead of providing the handcrafted html update the question with the relevant text based html. – undetected Selenium Jun 02 '20 at 11:10
  • It is weird. When I print the HTML file using: "`print(driver.page_source)`", I get a different HTML than the one I am inspecting on chrome. Maybe this is why I am not able to click on the button? I cannot update the question with the printed HTML file because it contains sensitive information. I will do my best to employ all what I learned from your answers and try to solve this and update the page with the answer (if I reach one) to help others with the same problem. – Azazil Jun 03 '20 at 11:11

3 Answers3

0

Try an xpath like:

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//ul/li/button[@class='icon-button has tooltip']"))).click()

This is based strictly on the html you have shared, in other words, assuming the xpath will match just one item on the page.

Required imports:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
0buz
  • 3,443
  • 2
  • 8
  • 29
  • Thank you for your answer. I appreciate it. It couldn't find the element I am looking for. "NoSuchElementException" was raised. I will try to expand more on it and deal with the button with the same method you tried with xpath. Is xpath the right answer here? – Azazil Jun 02 '20 at 10:22
  • Try waiting before attempting the click (see update). `xpath` is one way. Depends on the html structure on that page and what information is available there to play with. – 0buz Jun 02 '20 at 10:31
0

Use 'contains method' in the XPath as mentioned below.

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//ul/li/button[contains(@class,'icon-button has tooltip')]"))).click()
Vemula Dheeraj
  • 64
  • 1
  • 11
  • Thank you for your solution. It did not work. I printed the HTML file using `print(driver.page_source)` and weirdly the file printed is different than the file I am inspecting on Chrome. Probably this is why I cannot find the element. I searched the printed file for it and could not find any clue to which one it is. I will keep trying and post updates. – Azazil Jun 03 '20 at 11:15
0

We need to use javascript to click the element because selenium does not classify them as “interactable”:

expand_button_xpath = 'put-xpath-here'
element = driver.find_element_by_xpath(expand_button_xpath)
driver.execute_script("arguments[0].click();", element)

With AJAX:

timeout = 30
expand_button_xpath = 'put-xpath-here'
element = WebDriverWait(driver, timeout).until(
    expected_conditions.presence_of_element_located((By.XPATH,
                                                     expand_button_xpath)))
driver.execute_script("arguments[0].click();", element)