Haze
Haze

Reputation: 11

Change style with selenium python

On this website I try to set some filters to collect data but I can't access to the table using a click event with selenium in my python script. I noticed that I need to change the style from :

div id="filtersWrapper" class="displayNone " style="display: none;"

to

div id="filtersWrapper" class="displayNone " style="display: block;"

I think that I should use driver.execute_script(), but I have no clue how to do it

I would greatly appreciate some help with this. Thank you!

Upvotes: 1

Views: 2436

Answers (3)

CEH
CEH

Reputation: 5909

You can use driver.execute_script() to accomplish this. This is how I change the style attribute in my own code:

div_to_change = driver.find_element_by_id("filtersWrapper")
driver.execute_script("arguments[0].style.display = 'block';", div_to_change)

I had a look at the website you are automating, and you might not need to use JSE at all to do this - there's a reason the div you are trying to click has style = "display: none" - it is not meant to be clicked in this context. Working around that with Javascript might not produce your intended results. This code snippet has been updated with your requirements to set a Time filter in the Economic Calendar section:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver.get("https://www.investing.com/economic-calendar/")

driver.find_element_by_id("economicCurrentTime").click()

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "filterStateAnchor"))).click()

checkbox_for_bull3 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@id='importance2']")))
driver.execute_script("arguments[0].scrollIntoView(true);", checkbox_for_bull3)
checkbox_for_bull3.click()

checkbox_for_time = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//fieldset[label[@for='timeFiltertimeOnly']]/input")))
checkbox_for_time.click()

I modified your code snippet to fix a few issues -- when navigating to the economic-calendar page, you were clicking the 'Filters' field twice which caused an issue trying to click checkbox_for_bull3. I also added a scrollIntoView() Javascript call.

I ran this on my local machine and the code executed end to end successfully.

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193088

I have checked the DOM Tree of the webpage. Somehow I was unable to locate any element as:

<div id="filtersWrapper" class="displayNone " style="display: none;">

However the following element exists:

<div id="filtersWrapper" class="displayNone ">
        <div id="filtersArrowIndicator" class="arrowIndicator"></div>
    .
    <div id="economicCalendarSearchPopupResults" class="eventSearchPopupResults economicCalendarSearchPopupResults text_align_lang_base_1 dirSearchResults calendarFilterSearchResults displayNone">
    </div>
</div>

Not sure if that was your desired element. A bit of more information about your usecase would have helped us to debug the issue in a better way. However, To set the display property of style attribute as block for the element you can use:

driver.execute_script("document.getElementById('filtersWrapper').style.display='block';");

Upvotes: 0

RKelley
RKelley

Reputation: 1119

You can change an attribute on an element using javascript through selenium

element = driver.find_element_by_id('filtersWrapper')
driver.execute_script("arguments[0].setAttribute('attributeToChange', 'new value')", element)

or you can try clicking the element with javascript

driver.execute_script("arguments[0].click();", element)

Upvotes: 1

Related Questions