在运行下面的代码时,我得到了一个“找不到元素”的异常。我的预期输出是“未找到Button。这不应该发生。”我的代码:
driver = webdriver.Chrome()
driver.get(byPassUrl)
driver.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[2]/div/div[1]/div[2]/div/div[1]/div[1]/span/button").click()
time.sleep(3)
emails = driver.find_element_by_css_selector("input[type='email']")
emails.send_keys("mymail.com")
driver.find_element_by_css_selector("input[type='password']").send_keys("abcxyz")
driver.find_element_by_xpath('//button[text()="Log In"]').click()
time.sleep(3)
def delay(waiting_time=5):
driver.implicitly_wait(waiting_time)
audioBtnFound = False
def bypasscaptcha():
try:
# switch to recaptcha audio control frame
driver.switch_to.default_content()
frames = driver.find_element_by_xpath("/html/body/div[3]/div[2]").find_elements_by_tag_name("iframe")
driver.switch_to.frame(frames[0])
delay()
driver.find_element_by_id("recaptcha-audio-button").click()
# get the mp3 audio file
src = driver.find_element_by_id("audio-source").get_attribute("src")
print("[INFO] Audio src: %s" % src)
# download the mp3 audio file from the source
urllib.request.urlretrieve(src, os.path.normpath(os.getcwd() + "\\sample.mp3"))
delay()
# load downloaded mp3 audio file as .wav
try:
sound = pydub.AudioSegment.from_mp3(os.path.normpath(os.getcwd() + "\\sample.mp3"))
sound.export(os.path.normpath(os.getcwd() + "\\sample.wav"), format="wav")
sample_audio = sr.AudioFile(os.path.normpath(os.getcwd() + "\\sample.wav"))
except Exception:
print("[ERR] Please run program as administrator or download ffmpeg manually, "
"http://blog.gregzaal.com/how-to-install-ffmpeg-on-windows/")
# translate audio to text with google voice recognition
r = sr.Recognizer()
with sample_audio as source:
audio = r.record(source)
key = r.recognize_google(audio)
print("[INFO] Recaptcha Passcode: %s" % key)
delay()
# key in results and submit
driver.find_element_by_id("audio-response").send_keys(key.lower())
driver.find_element_by_id("audio-response").send_keys(Keys.ENTER)
except Exception as e:
print(e)
try:
driver.switch_to.default_content()
frames = driver.find_element_by_xpath("/html/body/div[3]/div[2]").find_elements_by_tag_name("iframe")
driver.switch_to.frame(frames[0])
delay()
driver.find_element_by_id("recaptcha-audio-button").click()
audioBtnFound = driver.find_element_by_id("recaptcha-audio-button").is_displayed()
except Exception as e:
print("audioBtn not found")
num = 1
if audioBtnFound:
try:
while True:
print(num)
num += 1
bypasscaptcha()
delay()
#if eye button found do bypasscaptcha
try:
driver.find_element_by_id("recaptcha-image-button").click()
print("Fail captcha")
delay()
bypasscaptcha()
except Exception as e:
print("Pass captcha")
audioBtnFound = False
break
except Exception as e:
print(e)
print('Caught. Need to change proxy now')
else:
print('Button not found. This should not happen.')如果我运行上面的代码,我会得到"Unable to Locate Element Exception".I know,当我登录时,不需要绕过验证码,这个元素就找不到了“
driver.find_element_by_xpath("/html/body/div[3]/div[2]").find_elements_by_tag_name("iframe")但是我该如何处理这个问题呢?非常感谢
发布于 2021-09-02 18:20:37
可以使用tag name或//iframe xpath直接找到iframes标记。您不必在/html/body/div[3]/div[2]之后查找它们
直接尝试:
all_iframes = find_elements_by_tag_name("iframe")现在你在页面上有了iframe的列表。
如果您知道这个数字,那么您可以像切换代码一样切换它。
发布于 2021-09-03 16:18:13
可能是selenium没有看到它需要的标签,或者您注册了一个不正确的搜索。按id或css_selector设置搜索。不要在一行代码中做所有的事情,首先要找到
driver.find_element_by_xpath("/html/body/div[3]/div[2]")然后寻找
driver.find_elements_by_tag_name("iframe")https://stackoverflow.com/questions/69033669
复制相似问题