我在网页上有几个列表,例如以下示例。第一个清单显示一个“立即申请”按钮,我可以单击下面的代码。然后,在我填充一个对话框并关闭它之后,第一个列表从页面中消失,其他列表仍然可见。但是,直到我单击下一个列表中的任意位置,“下一个应用”按钮才会出现在下一个列表中,然后该列表就会弹出“立即应用”按钮。另外,如果我刷新页面,则会弹出“立即申请”按钮以显示下一个列表。如果我尝试第二次在下面运行代码(当下一个列表中“立即申请”按钮不可见时),我将收到以下错误消息。我的目标是能够为每个列表单击“立即申请”,一个又一个。任何人都可以建议如何使“立即申请”按钮出现在下一个列表中,在本示例中为“ SecondOne”。我已经尝试过下面的代码,但是两次尝试都没有。
第一个列表:
DbContext
第二个列表:
<div data-id="972688" data-name="FirstOne" data-force-note="0" data-force-note-on-archive="0" data-allowed-to-mark-interest="1" data-listing-ids="[256855]" data-in-network="false" data-promoted="false" class="djl87 thing_listings fbw9 browse_startups_table_row _a _jm expanded" data-_tn="thing_listings/browse_startups_table_row" _js-initialized="true">
<div class="js-action-bar-placeholder action-bar-placeholder u-hidden"></div>\n<div class="action-bar browse_startup_action_bar" style="display: block;">\n<table>\n<tbody><tr>\n<td class="top-left-corner">\n<div class="star">\n<span>☆</span>\n</div>\n</td>\n<td class="right-corner">\n<div class="prompt">\n<div class="s-vgBottom0_5 u-hidden u-fontSize12 u-fontWeight400 u-textAlignLeft js-action-bar-details">\n<div class="action-bar-pic s-vgRight0_5 u-inlineBlock">\n<img alt="" src="https://d1qb2nb5cznatu.cloudfront.net/startups/i/972688-67bea67278d8a1b824cf5df5fa8dc1ae-thumb_jpg.jpg?buster=1522805890" />\n</div>\nWant to talk to FirstOne?\n</div>\n<div class="buttons">\n<a class="g-button blue apply-now-button">\nApply Now\n</a>
代码:
<div data-id="1126380" data-name="SecondOne" data-force-note="0" data-force-note-on-archive="0" data-allowed-to-mark-interest="1" data-listing-ids="[451322]" data-in-network="false" data-promoted="true" data-promotion-event-id="20694189" data-verify-promotion-event-url="https://placeweb.co/thing_ads/impressions/20694189/verify" data-click-promotion-event-url="https://placeweb.co/thing_ads/impressions/20694189/click" class=" djl87 thing_listings fbw9 browse_startups_table_row _a _jm" data-_tn="thing_listings/browse_startups_table_row" _js-initialized="true">
<div class="js-action-bar-placeholder action-bar-placeholder u-hidden"></div>\n<div class="action-bar browse_startup_action_bar">\n<table>\n<tbody><tr>\n<td class="top-left-corner">\n<div class="star">\n<span>☆</span>\n</div>\n</td>\n<td class="right-corner">\n<div class="prompt">\n<div class="s-vgBottom0_5 u-hidden u-fontSize12 u-fontWeight400 u-textAlignLeft js-action-bar-details">\n<div class="action-bar-pic s-vgRight0_5 u-inlineBlock">\n<img alt="" src="https://d1qb2nb5cznatu.cloudfront.net/startups/i/1126380-9ca4e0e38d777ea699f703fe7db28cfb-thumb_jpg.jpg?buster=1537381145" />\n</div>\nWant to talk to SecondOne?\n</div>\n<div class="buttons">\n<a class="g-button blue apply-now-button">\nApply Now\n</a>
错误:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.g-button.blue.apply-now-button"))).click()
# clicks first "Apply Now" button
# driver.find_element_by_css_selector('.g-button.blue.apply-now-button').click()
# time.sleep(3)
答案 0 :(得分:0)
请尝试以下选项以单击元素。
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()[contains(.,'Apply Now')]]"))).click()
OR
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.g-button"))).click()
如果在上述两个选项中均未引发任何错误,而您仍然无法单击,请尝试使用JavaScripts
执行器。
element=WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()[contains(.,'Apply Now')]]")))
driver.execute_script("arguments[0].click();", element)
让我知道这是否可行。