我实际上制作了#34; Facebook添加朋友脚本",在"添加朋友"按钮。所有工作都没有问题,但有时Facebook弹出窗口(帮助用户"名称"找朋友)会中断我的脚本。我有一个想法:检查所有For循环显示弹出窗口,但它会非常慢(设置sleep.wait(s)并等待web元素加载)。 什么阻止它?这是唯一的例子,我觉得很容易理解:)
friendbuttons = driver.find_elements_by_xpath(button xpath)
x=0
for friendbutton in friendbuttons:
wait and check for popup(By popup.is.displayed?)
if popup display
close popup
else
friendbutton.click()
x = x + 1
print("Add " + str(x) + " friend")
time.sleep(1)
if (i > 20):
break
Facebook popup
<div class="_59s7" role="dialog" aria-labelledby="u_13_1" style="width: 480px; margin-top: 40px;"><div class="_4t2a"><div style="opacity: 1;"><div><div class="_4-i0"><div class="clearfix"><div class="_51-u rfloat _ohf"><a class="_42ft _5upp _50zy layerCancel _51-t _50-0 _50z-" role="button" href="#" title="Zamknij" data-testid="dialog_title_close_button">Zamknij</a></div><div><h3 id="u_13_1" class="_52c9"><div class="_1t0"><div class="ptm title fwb">Zasugeruj użytkownikowi Szymon znajomych</div>
&#13;
答案 0 :(得分:1)
您可以尝试在下面实施:
from selenium.common.exceptions import ElementNotVisibleException
from selenium.common.exceptions import NoSuchElementException
try:
driver.find_element_by_link_text('Zamknij').click()
except (ElementNotVisibleException, NoSuchElementException):
friendbutton.click()
....
如果出现弹出窗口,则应该允许单击“关闭”按钮,否则单击“添加好友”