tew
tew

Reputation: 281

Selenium Python: How to wait until an element finish changing the text before reading the value using?

I am trying to read the value of a text from a page using Selenium Python. The element is always visible and then goes invisible and then becomes visible and the text value changes rapidly until it reaches the final value. I think it is using some form of javascript to get the value before displaying the final value.

The page is https://www.junglescout.com/estimator/

I am trying to get the result of the estimate from the element js-magic-result.

I'm able to use Selenium to fill the three forms and click the 'calculates sales' button

I am using the chromedriver in python selenium to read the value.

all the tests gets me one of the intermediate values before it finishes loading.

I tried using the folling inplace of the browser2.implicitly_wait(5):

driver.implicitly_wait(5)
wait = WebDriverWait(browser2,3)
wait.until(EC.invisibility_of_element_located((By.CLASS_NAME,'js-magic-result')))
wait.until(EC.visibility_of_element_located((By.CLASS_NAME,'js-magic-result')))

Here is the full code I am using

browser2 = webdriver.Chrome(options=options,executable_path=driverPath)
url = 'https://www.junglescout.com/estimator/'
browser2.get(url)

container = browser2.find_element_by_class_name('js-estimation-section')
rankField = container.find_element_by_name('theRankInput')
rankField.send_keys('345')

# Click for storesList drop down
storeGroup = container.find_element_by_class_name('js-est-stores-list-input')
storeGroup.find_element_by_class_name('x-icon-caret-down').click()

# Get Stores List items
wait = WebDriverWait(browser2,3)
stores = wait.until(EC.visibility_of_element_located((By.CLASS_NAME,'js-est-stores-list')))
stores.find_elements_by_tag_name('span')[0].click()

# Wait for storesList is invisible
wait.until(EC.invisibility_of_element_located((By.CLASS_NAME,'js-est-stores-list')))

# Click for Categories list drop down
catGroup = container.find_element_by_class_name('js-est-categories-list-input')
catGroup.find_element_by_tag_name('input').click()

# Get Categories List items
cats = wait.until(EC.visibility_of_element_located((By.CLASS_NAME,'js-est-categories-list')))

# Get Categories List items
for cat in cats.find_elements_by_class_name('us-available'):
    if (cat.find_element_by_tag_name('span').text == 'Electronics'):
        cat.click()
        break
# Wait for storesList is invisible
wait.until(EC.invisibility_of_element_located((By.CLASS_NAME,'js-est-categories-list')))

# wait5 = WebDriverWait(browser2,3)
submit = wait.until(EC.visibility_of_element_located((By.CLASS_NAME,'js-est-btn')))
submit.click()

browser2.implicitly_wait(5)
print(container.find_element_by_class_name('js-magic-result').text)

What I expect is to get the final value returned but what I get is one of the intermediate values from the element.

Upvotes: 0

Views: 674

Answers (1)

KunduK
KunduK

Reputation: 33384

Instead Of

print(container.find_element_by_class_name('js-magic-result').text)

Please try this.

print(browser2.find_element_by_xpath("//table[@class='js-estimation-section']//tbody//tr/td[2]/p").text)

Make sure before print some delay should be there so please replace only print code.

Let me know if this works.

Upvotes: 2

Related Questions