问题
Im trying to upload an image to facebook and im unable to click on the Add photo and video button.
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">
im trying to find the elment by id:
driver.find_elment_by_id("js_17y").click()
and im getting:
selenium.common.exceptions.NoSuchElementException: Message: no such element
回答1:
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:
Using
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()
Using
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
回答2:
your id might getting change everytime.Try use xpath with attribute.
However use webdriverwait
and element_to_be_clickable
to click on that.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//input[@aria-label="Add Photo or Video"][@name="composer_photo[]"]'))).click()
回答3:
Thanks guys i was able to find it by the class name trough the parent
driver.find_element_by_class_name("_3jk")
来源:https://stackoverflow.com/questions/56684085/python-selenium-cant-find-the-add-photo-and-video-button-element-on-facebook