问题
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:
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"]')))
来源:https://stackoverflow.com/questions/41206797/select-javascript-created-element-in-selenium-python