我在网页上有几个列表,如下面的示例。第一个列表显示“立即应用”按钮,我可以使用下面的代码单击该按钮。然后在我填充对话框并关闭它后,第一个列表从页面中消失,其他列表仍然可见。但是,在我点击下一个列表中的任何位置之前,“立即申请”按钮不会显示在下一个列表中,然后将弹出“立即应用”按钮以进行列表。此外,如果我刷新页面,则会弹出“立即应用”按钮以进行下一个列表。如果我第二次尝试运行下面的代码,(当下一个列表中看不到“立即应用”按钮时),我将得到以下错误。我的目标是能够为每个列表单击“立即应用”,一个接一个。任何人都可以建议如何在下一个列表中显示“立即应用”按钮,在此示例中为“SecondOne”。我已经尝试了下面的代码,但两次尝试都没有重复。
第一次上市:
<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)
错误:
---------------------------------------------------------------------------
TimeoutException Traceback (most recent call last)
<ipython-input-46-792b206b8d3d> in <module>()
3 from selenium.webdriver.support import expected_conditions as EC
4
----> 5 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.g-button.blue.apply-now-button"))).click()
6
7
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/support/wait.py in until(self, method, message)
78 if time.time() > end_time:
79 break
---> 80 raise TimeoutException(message, screen, stacktrace)
81
82 def until_not(self, method, message=''):
TimeoutException: Message:
请尝试以下选项以单击元素。
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()[contains(.,'Apply Now')]]"))).click()
要么
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)
让我知道这个是否奏效。