简体   繁体   中英

WebElement (Selenium/Python)

I'm trying to upload a image to https://www.alibaba.com/ through Selenium.

So i find the element which allows me to do that:

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 image
wait =  WebDriverWait(driver, 5)
x = True
while 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'))

When i print out searcher1 i get:

<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>

Max 2MB per Image

That's the element i need to upload the image, but when i try to do the following:

Option 1

searcher1.find_element_by_class_name('.moxie-shim moxie-shim-html5')

Option 2

searcher1.find_element_by_class_name('upload-btn')

Option 3

searcher1.find_element_by_xpath('/div')

I get the following (for option 3, for example):

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/div"}

What's the problem? I'm stuck :(

For relative xpath, you need to put a . in front of it. Try:

searcher1.find_element_by_xpath('./div')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM