Python Selenium在Facebook上找不到添加照片和视频按钮元素 [英] Python Selenium cant find the Add photo and video button element on Facebook

查看:68
本文介绍了Python Selenium在Facebook上找不到添加照片和视频按钮元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像上传到Facebook,但无法单击添加照片和视频"按钮.

Im trying to upload an image to facebook and im unable to click on the Add photo and video button.

当我查看html时,这是我要点击的元素:

When im looking at the html this is the element im trying to click:

<input aria-label="Add Photo or Video" accept="video/*,  video/x-m4v, 
video/webm, video/x-ms-wmv, video/x-msvideo, video/3gpp, video/flv, 
video/x-flv, video/mp4, video/quicktime, video/mpeg, video/ogv, .ts, .mkv, 
image/*, image/heic, image/heif" containerclassname="_5g_r" multiple="" 
name="composer_photo[]" display="inline" role="button" tabindex="0" data- 
testid="media-sprout" type="file" class="_n _5f0v" id="js_17y">

我正在尝试通过ID查找要素:

im trying to find the elment by id:

driver.find_elment_by_id("js_17y").click()

即时通讯:

selenium.common.exceptions.NoSuchElementException: Message: no such element

推荐答案

Facebook 通过 ReactJS构建,因此要对元素进行 click(),您需要诱使 WebDriverWait 使元素可点击,并且您可以使用以下任一方法以下定位器策略:

Facebook is built through ReactJS so to click() on the element you need to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • 使用 css_selector :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[aria-label='Add Photo or Video'][name^='composer_photo'][data-testid='media-sprout']"))).click()

  • 使用 xpath :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-label='Add Photo or Video' and starts-with(@name, 'composer_photo')][@data-testid='media-sprout']"))).click()
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

  • 这篇关于Python Selenium在Facebook上找不到添加照片和视频按钮元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆