3

I have the following element in a web page.

<button type="submit" class="zsg-button_primary contact-submit-button track-ga-event" data-ga-category="contact" data-ga-action="email" data-ga-label="rentalbuilding" data-ga-event-content="false" data-ga-event-details="" id="yui_3_18_1_2_1482045459111_1278">
   <span class="zsg-loading-spinner hide"></span>
   <span class="button-text" id="yui_3_18_1_2_1482045459111_1277">Contact Property Manager</span>
</button>

I can find this element with Beautifulsoup using:

e = soup.find('button', attrs={'class' : 'zsg-button_primary'})

But when I try with Selenium using:

driver.find_element_by_class_name("zsg-button_primary").click()

I get the following error:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

I think this is because the element is being created in Javascript and is doing something funny but I can see it on the screen and I just don't know how to get it so I can click it. Any help most welcome.

EDIT

I've been pointed toward this answer which is for Selenium Javascript. I need to do this with Python is anyone can help.

1

1 Answer 1

1

Try to wait until your element become visible with following:

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

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//button[@class="zsg-button_primary contact-submit-button track-ga-event"]')))
Sign up to request clarification or add additional context in comments.

5 Comments

Surely it is already visible because it is being picked up by BeautifulSoup?
@HenryM, actually, no. bsp handles scraped HTML source, while selenium- browser window. Presence in page source doesn't mean that element is visible
so how do I make it visible because without a prompt it just sites there
Your locator is too broad. find_element_by_class_name("zsg-button_primary") will find first element with specified class name. while there could be more than one element with this attribute. Does len(driver.find_elements_by_class_name("zsg-button_primary")) returns 1?
No, you're right. I need to get the first button with that class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.