为了账号安全,请及时绑定邮箱和手机立即绑定

WebElement (Selenium/Python)

WebElement (Selenium/Python)

偶然的你 2021-09-11 15:43:05
我正在尝试通过 Selenium将图像上传到https://www.alibaba.com/。所以我找到了允许我这样做的元素:driver = webdriver.Chrome(r'C:\Users\migue\Desktop\WorkerBot\Drivers\chromedriver')driver.maximize_window()driver.get('https://www.alibaba.com/');time.sleep(5)#Open menu to upload imagewait =  WebDriverWait(driver, 5)x = Truewhile x:    x = False    try:        search_camara = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'i.ui-searchbar-imgsearch-icon')))         search_camara.click()    except:        x = True        driver.refresh()time.sleep(5)searcher1 = driver.find_element_by_xpath('//*[@id="J_SC_header"]/header/div[2]/div[2]/div/div/form/div[2]/div[3]/div[1]/div/div')print(searcher1.get_attribute('innerHTML'))当我打印出 searcher1 时,我得到:<div class="upload-btn-wrapper"><div class="upload-btn" style="z-index: 1;">Upload Image</div><div id="html5_1cu85jlnu116m14sle1omtch5s3_container" class="moxie-shim moxie-shim-html5" style="position: absolute; top: 14px; left: 183px; width: 109px; height: 28px; overflow: hidden; z-index: 0;"><input id="html5_1cu85jlnu116m14sle1omtch5s3" type="file" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;" multiple="" accept="image/jpeg,image/png,image/bmp"></div>每张图片最多 2MB这是我需要上传图像的元素,但是当我尝试执行以下操作时:选项1searcher1.find_element_by_class_name('.moxie-shim moxie-shim-html5')选项 2searcher1.find_element_by_class_name('upload-btn')选项 3searcher1.find_element_by_xpath('/div')我得到以下信息(例如,对于选项 3):NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/div"}有什么问题?我被卡住了:(
查看完整描述

1 回答

?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

对于相对 xpath,你需要.在它前面放一个。尝试:

searcher1.find_element_by_xpath('./div')


查看完整回答
反对 回复 2021-09-11
  • 1 回答
  • 0 关注
  • 148 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信