在Selenium Python中选择Javascript创建的元素 [英] Select Javascript created element in Selenium Python

查看:51
本文介绍了在Selenium Python中选择Javascript创建的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页中具有以下元素.

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>

我可以使用Beautifulsoup使用以下方法找到该元素:

I can find this element with Beautifulsoup using:

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

但是当我尝试使用Selenium时:

But when I try with Selenium using:

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

我收到以下错误:

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

我认为这是因为该元素是用Javascript创建的,并且正在做一些有趣的事情,但是我可以在屏幕上看到它,但是我不知道如何获得它,所以我可以单击它.任何帮助都非常欢迎.

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.

编辑

我已经指向

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

推荐答案

尝试通过以下操作等待元素可见:

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"]')))

这篇关于在Selenium Python中选择Javascript创建的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆