这是几个星期前的工作,我测试的网站没有改变任何东西。有时它会突出显示我需要的文字,但它不会将其复制到剪贴板,有时它会起作用;很少现在,但每次我写它时它都正常工作,而且没有做time.sleep()。我现在已经开始收到错误消息了。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Firefox()
driver.get("http://www.speedyshare.com/")
driver.switch_to.frame(0)
upload_input = driver.find_element_by_css_selector(
"#selectfilebox input[type=file]")
upload_input.send_keys("/home/ro/Documents/Aaatrack.pdf")
upload_button = driver.find_element_by_link_text("Upload")
upload_button.click()
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "inpfilelink")))
elem = driver.find_element_by_class_name("inpfilelink")
elem.send_keys(Keys.CONTROL, "a")
time.sleep(3)
elem.send_keys(Keys.CONTROL, "c")
这是我有时会得到的错误
Traceback (most recent call last):
File "file wrap.py", line 27, in <module>
elem.send_keys(Keys.CONTROL, "a")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 328, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 454, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmp8lWb_m/extensions/fxdriver@googlecode.com/components/command-processor.js:9981)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmp8lWb_m/extensions/fxdriver@googlecode.com/components/command-processor.js:12517)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmp8lWb_m/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmp8lWb_m/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///tmp/tmp8lWb_m/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
我已经用萤火虫检查了该元素,但它仍然存在同名。
答案 0 :(得分:0)
Problems happens because when you hover over the Download link, an alert pops up and shows
按CTRL + C立即复制到剪贴板。
对我来说,下面的代码工作得非常棒! -
打印上传文档的网络共享链接,并将复制的链接粘贴到下面的文本区域//*[@id='linklisttextarea']
。所以它变成了四次!
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Firefox()
driver.get("http://www.speedyshare.com/")
driver.maximize_window()
driver.switch_to.frame(0)
upload_input = driver.find_element_by_css_selector(
"#selectfilebox input[type=file]")
upload_input.send_keys("C:\abc.pdf")
upload_button = driver.find_element_by_link_text("Upload")
upload_button.click()
element = WebDriverWait(driver, 100).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='linklisttextarea']")))
elem = driver.find_element_by_xpath("//td[contains(text(),'Download Link:')]/following-sibling::td[1]/input")
share_lnk =elem.get_attribute('value')
print share_lnk
lnk = driver.find_element_by_xpath("//input[@class='inpfilelink']")
print "Your web linkk is %s"%(lnk.get_attribute('value'))
txt = driver.find_element_by_xpath("//*[@id='linklisttextarea']")
txt.send_keys(share_lnk)
txt.send_keys(share_lnk)
txt.send_keys(share_lnk)
txt.send_keys(share_lnk)
driver.quit()