选择seleniumpython中Javascript创建的元素

2024-06-17 11:53:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在网页中有以下元素。在

<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>

我可以用beauthulsoup找到这个元素:

^{pr2}$

但当我尝试使用硒时:

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

我得到以下错误:

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

我想这是因为这个元素是用Javascript创建的,而且做了一些有趣的事情,但是我可以在屏幕上看到它,我只是不知道如何获取它,所以我可以单击它。欢迎任何帮助。在

编辑

我已经指出了this answer,它是针对Selenium Javascript的。我需要用Python做这个是任何人都可以帮助的。在


Tags: eventid元素datacontactbuttonelementjavascript
1条回答
网友
1楼 · 发布于 2024-06-17 11:53:08

请尝试等待,直到您的元素显示以下内容:

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

相关问题 更多 >