我要替换文字的元素:
<input tabindex="1" style="padding-left:119px!important;width:318px!important;background-color:#fff;outline: none;box-sizing: inherit;" id="email" name="mobile" placeholder="Enter your Mobile Number" class="un_s un1_s" value="" onblur="remove_border();" type="text" maxlength="20">
我尝试了各种方法(我只能想到和google),以替换placeholder
文字。之后很多错误,我想,因为焦点已经在文本框中(我想要输入文本可能只是send_keys可以工作。它没有。可以有人帮我解释概念或指向我可能会在哪里读到我出错的地方?
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_path = r"C:\Users\-------\Desktop\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("SAMPLE WEBSITE URL") #sorry had to remove the link :(
driver.maximize_window()
action = webdriver.ActionChains(driver)
elm = driver.find_element_by_id("user_sign_in").click()
inputElement = driver.find_element_by_xpath('//*[@id="lfm"]/div[1]/div[2]') #driver.find_elements_by_xpath("//*[contains(text(), 'Enter your Mobile Number')]")
#driver.find_element_by_id("mobile")
inputElement.send_keys('1234567890')
inputElement.submit()
#//*[@id="lfm"]/div[1]/div[2] xpath id for the mobile number element
#code below this is not working, for move mouse
#action = webdriver.ActionChains(driver)
#action.move_to_element((By.XPATH, '//*[@id="user_sign_in"]')).perform()
#For moving the mouse to sign in: Tried the ones below and they didn't work either
#driver.move_to_element(By.XPATH, '//*[@id="user_sign_in"]')
#login_menu = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="user_sign_in"]')))
#ActionChains(driver).move_to_element(sign_in).perform()
错误:
追踪(最近一次通话): File&#34; C:/ Python34 / Selenium 2nd Trial.py&#34;,第16行,in inputElement.send_keys(&#39; 9810307369&#39;) 文件&#34; C:\ Python34 \ lib \ site-packages \ selenium \ webdriver \ remote \ webelement.py&#34;,第322行,在send_keys中 self._execute(Command.SEND_KEYS_TO_ELEMENT,{&#39; value&#39 ;: keys_to_typing(value)}) 文件&#34; C:\ Python34 \ lib \ site-packages \ selenium \ webdriver \ remote \ webelement.py&#34;,第457行,在_execute中 return self._parent.execute(command,params) 文件&#34; C:\ Python34 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py&#34;,第233行,执行 self.error_handler.check_response(响应) 文件&#34; C:\ Python34 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py&#34;,第194行,在check_response中 提出exception_class(消息,屏幕,堆栈跟踪) selenium.common.exceptions.WebDriverException:消息:未知错误:无法聚焦元素 (会议信息:chrome = 55.0.2883.87) (驱动程序信息:chromedriver = 2.27.440174(e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform = Windows NT 10.0.14393 x86_64)
答案 0 :(得分:0)
您正在尝试将密钥发送到div
元素,但意味着使用input
元素:
inputElement = driver.find_element_by_id('email')
inputElement.send_keys('1234567890')
inputElement.submit()
并且,请注意页面可能需要一些时间来加载,并且可能存在阻止元素定位或可交互的视觉效果。如果是这种情况,请使用WebDriverWait
和适当的预期条件,以确保元素可见。