【发布时间】:2021-10-27 23:55:06
【问题描述】:
我正在寻找能够使用 selenium python 自动按住按钮的元素 xpath ,但是我遇到了困难,因为它们在 iframe 中。 我尝试了“Expected_conditions as EC”,但我的知识不足以让它们发挥作用。告诉我要改变什么,或更明智的方法。
HTML:
<div id="px-captcha" role="main">
<iframe style="display: none; width: 310px; height: 100px; border: 0px; user-select: none;" token="951d7e81fd6fb5e2af2cb2c701dbb6c391ab81d4b983da5f2f2de85667241a43a3a814a87cae2e98c70b730f7eaaac0a04bbf77bbfc63735e436d1d07675cb68"></iframe>
<iframe style="display: block; width: 310px; height: 100px; border: 0; -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; -ms-user-select: none; user-select: none;" token="951d7e81fd6fb5e2af2cb2c701dbb6c391ab81d4b983da5f2f2de85667241a43a3a814a87cae2e98c70b730f7eaaac0a04bbf77bbfc63735e436d1d07675cb68">
#document
<html lang="en-US"
<head>...</head>
<body>
<div id="kkBSsePnKDMVkwa" class="eIlUWbNLSMdFkEz">
<div id="#LrJbZYBfdAzlAkl"></div>
<div id="BlXIkuwFPcwvDCY" role="main" aria-label="Please press and hold the button until verified">...</div>
</div>
</body>
</html>
</iframe>
<iframe style="display: none; width: 310px; height: 100px; border: 0px; user-select: none;" token="951d7e81fd6fb5e2af2cb2c701dbb6c391ab81d4b983da5f2f2de85667241a43a3a814a87cae2e98c70b730f7eaaac0a04bbf77bbfc63735e436d1d07675cb68"></iframe>
<p style="color: red; margin-top: 4;">Please try again</p>
</div>
代码(更新)#错误:
def captcha(url):
driver.get(str(url))
time.sleep(10)
try:
captcha_element = driver.find_element_by_id('px-captcha')
print(len(captcha_element.text), 'Captcha verification request page')
print('Run pass captcha programing')
# 2.1: Verify captcha
# Research iframe containing captcha
# # Example 2: Use pyautogui library
# driver.set_window_position(0, 0)
# driver.set_window_size(1024, 640)
# sleep(randint(5,10))
# pyautogui.moveTo(400, 438)
# pyautogui.click()
# pyautogui.dragTo(596, 438, 5, button='left')
# Example 3:
for i in range(10):
try:
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "(//div[@id='px-captcha']/iframe)[{i}]"))) #ERROR HERE. TO TRY REPLACEMENT <wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "(//div[@id='px-captcha']/iframe)[2]")))>
print ("- Found iframe")
element = driver.find_element(By.XPATH, "//div[contains(@aria-label, 'Please press and hold the button until verified')]")
print ("- Found element")
# click and hold 5 seconds to pass the captcha
print("Button verify: ", len(element.text))
action = ActionChains(driver)
click = ActionChains(driver)
frame_x = element.location['x']
frame_y = element.location['y']
print("x: ", frame_x)
print("y: ", frame_y)
print("size box: ", element.size)
print("x max click: ", frame_x + element.size['width'])
print("y max click: ", frame_y + element.size['height'])
x_move = frame_x + element.size['width']/2
y_move = frame_y + element.size['height']/2
print("Click (x,y) = ", x_move, y_move)
action.move_to_element_with_offset(element, x_move, y_move).click_and_hold().perform()
time.sleep(10)
action.release(element)
action.perform()
time.sleep(0.2)
action.release(element)
print('Verify successful')
break
except:
print(f'- NOT Found xpath Num.: {i}')
sleep(randint(5,10))
except:
# 2.2: Skip captcha
print('Website does NOT require captcha verification')
sleep(randint(2,3))
我想要找到元素:
<div id="BlXIkuwFPcwvDCY" role="main" aria-label="Please press and hold the button until verified">...</div>
【问题讨论】:
-
通过标签名获取
iframes的数量列表。喜欢driver.findElements(By.tagName("iframe"));并使用特定的iframe进行切换。请告诉我结果。 -
到 TypeError: 'str' object is not callable
标签: python python-3.x selenium iframe captcha