我在Firefox中使用Selenium (我已经尝试过chrome,但这一次的成功率要低得多)。我试图定位一个元素(上传按钮),成功率远远低于可接受的百分比。
现在,我知道我可以尝试在输入中使用selenium,但是不管我做什么,它也不会找到它。所以我用AutoIt做上传部分。
frdriver.get('')
time.sleep(11)
try:
# el = WebDriverWait(frdriver, 15).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/header/div[2]/div[2]/div/div/form/i[2]')))
el = frdriver.find_element_by_xpath('/html/body/div[1]/header/div[2]/div[2]/div/div/form/i[2]')
try:
el.click()
print(el)
#ActionChains(frdriver).move_to_element(el).click().perform()
try:
#element = WebDriverWait(frdriver, 15).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/header/div[2]/div[2]/div/div/form/div[2]/div[3]/div[1]/div/div/div[1]')))
element = frdriver.find_element_by_xpath('/html/body/div[1]/header/div[2]/div[2]/div/div/form/div[2]/div[3]/div[1]/div/div/div[1]')
time.sleep(4)
try:
element.click()
#ActionChains(frdriver).move_to_element(element).click().perform()
autoit.run("/image.exe")
r = 100
except:
print('Firefox retries upload')
r += 1
except:
print('Firefox retries upload')
r += 1
except:
print('Firefox retries upload')
r += 1
except:
print('Firefox retries upload')
r += 1正如你所看到的,我尝试过很多种方法,每一种方法都取得了几乎相同的成功。我正在为这项工作使用一台远程windows机器,这确实可能是我的主意,但如果不最小化它,它的工作效果就会好得多。
还有一些其他的原因,因为某些原因,即使它没有成功地点击一个按钮,它也会绕过try/except并继续执行。
编辑页面的 html
<div class="ui-searchbar-keyword-panel ui-searchbar-keyword-hide ui-searchbar-static" style="z-index: 99; position: absolute; left: 0px; top: 35px;" data-widget-cid="widget-7">
<div class="ui-searchbar-img-search-box">
<div class="icbu-w-image-uploader-panel">
<div class="icbu-w-image-uploader-content">
<div class="upload-btn-wrapper">
<div class="upload-btn" data-spm-anchor-id="a2700.8293689.scGlobalHomeHeader.i1.2ce265aarlKA3D" style="z-index: 1;">Upload Image</div>
<div id="html5_1cjiqdvveht51pom1nbkqoiaop3_container" class="moxie-shim moxie-shim-html5" style="position: absolute; top: 14px; left: 224px; width: 109px; height: 28px; overflow: hidden; z-index: 0;">
<input id="html5_1cjiqdvveht51pom1nbkqoiaop3" 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>
</div>
<div class="response-text">Max 2MB per Image</div>
</div>
</div>
</div>
<div class="J-box-loadding-mask box-loadding-mask" data-role="loading" style="display: none;">
<div class="mh-mamo-loadding-box">
<i class="mh-mamo-icon-loadding"></i>
<span class="J-loading-text mh-mamo-loadding-text">Loading...</span>
</div>
</div>
</div>发布于 2018-07-29 10:46:27
您应该使用driver.implicitly_wait(X),其中X是数秒,而不是使用time.sleep和重试来重新发明轮子。这将增加selenium对页面加载的容忍度,并且您将在查找页面元素方面获得更好的成功。
发布于 2018-07-30 12:30:14
要找到与文本Upload Image相关的upload按钮,可以使用以下解决方案:
XPATH:
(驱动程序,20).until(EC.element_to_be_clickable((By.XPATH,"//div@class='upload-btn-wrapper'//div@class='moxie-shim moxie-shim-html5 5‘并以(@id,’html5_‘)/inputstarts with(@id,’html5_‘)’开始).click()CSS_SELECTOR:
20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,=WebDriverWait(驱动程序,upload_button“div.uppload-btn-包装器div.moxie-shim.moxie-shim-html5id^='html5_'>inputid^='html5_'"))).click() )注意事项:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EChttps://stackoverflow.com/questions/51578424
复制相似问题