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.

Community
  • 1
  • 1
HenryM
  • 5,557
  • 7
  • 49
  • 105
  • Possible duplicate of [How to force Selenium WebDriver to click on element which is not currently visible?](http://stackoverflow.com/questions/6101461/how-to-force-selenium-webdriver-to-click-on-element-which-is-not-currently-visib) – 宏杰李 Dec 18 '16 at 09:01

1 Answers1

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"]')))
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Surely it is already visible because it is being picked up by BeautifulSoup? – HenryM Dec 18 '16 at 09:13
  • @HenryM, actually, no. `bsp` handles scraped `HTML` source, while `selenium`- browser window. Presence in page source doesn't mean that element is visible – Andersson Dec 18 '16 at 09:20
  • so how do I make it visible because without a prompt it just sites there – HenryM Dec 18 '16 at 09:30
  • 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`? – Andersson Dec 18 '16 at 09:38
  • No, you're right. I need to get the first button with that class – HenryM Dec 18 '16 at 09:55