Select Javascript created element in Selenium Python

别来无恙 提交于 2021-01-27 13:05:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!