atom 1.54 无法从浏览器获取 xpath(带有 Firefox 的 selenium webdriver)
atom 1.54 can't fetch xpath from the browser (selenium webdriver with Firefox)
这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Firefox()
browser.get('https://pbx.voxbaysolutions.com/#/page/signin')
browser.find_element_by_xpath('//input[@ng-model="user.name"]').send_keys('thj')
这里是错误:
Traceback (most recent call last):
File "voxbay_automate.py", line 8, in <module>
browser.find_element_by_xpath('//input[@ng-model="user.name"]').send_keys('thj')
File "C:\Users\theju\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webd
river\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\theju\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webd
river\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\theju\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webd
river\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\theju\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webd
river\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[@ng-model="user.name"]
正在获取元素
<input type="text" name="name" ng-model="user.name" class="form-control ng-pristine ng-invalid ng-invalid-required ng-touched" placeholder="User email" required="" tabindex="0" aria-required="true" aria-invalid="true">
在发送密钥之前等待网络元素加载:
WebDriverWait(browser, 30).until(EC.visibility_of_element_located((By.XPATH, '//input[@ng-model="user.name"]'))).send_keys('thj')
需要这些导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
要将 字符序列 发送到 <input>
元素,您可以使用以下任一方法 :
使用CSS_SELECTOR
:
driver.find_element(By.CSS_SELECTOR, "a[name='name'][ng-model='user.name']").send_keys('thj')
使用XPATH
:
driver.find_element(By.XPATH, "//a[@name='name' and @ng-model='user.name']").send_keys('thj')
但是所需的元素是 Angular 元素,因此要将 字符序列 发送到您需要的元素为 引入 WebDriverWait 并且您可以使用以下任一项 :
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[name='name'][ng-model='user.name']"))).send_keys('thj')
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@name='name' and @ng-model='user.name']"))).send_keys('thj')
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Firefox()
browser.get('https://pbx.voxbaysolutions.com/#/page/signin')
browser.find_element_by_xpath('//input[@ng-model="user.name"]').send_keys('thj')
这里是错误:
Traceback (most recent call last):
File "voxbay_automate.py", line 8, in <module>
browser.find_element_by_xpath('//input[@ng-model="user.name"]').send_keys('thj')
File "C:\Users\theju\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webd
river\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\theju\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webd
river\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\theju\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webd
river\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\theju\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webd
river\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[@ng-model="user.name"]
正在获取元素
<input type="text" name="name" ng-model="user.name" class="form-control ng-pristine ng-invalid ng-invalid-required ng-touched" placeholder="User email" required="" tabindex="0" aria-required="true" aria-invalid="true">
在发送密钥之前等待网络元素加载:
WebDriverWait(browser, 30).until(EC.visibility_of_element_located((By.XPATH, '//input[@ng-model="user.name"]'))).send_keys('thj')
需要这些导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
要将 字符序列 发送到 <input>
元素,您可以使用以下任一方法
使用
CSS_SELECTOR
:driver.find_element(By.CSS_SELECTOR, "a[name='name'][ng-model='user.name']").send_keys('thj')
使用
XPATH
:driver.find_element(By.XPATH, "//a[@name='name' and @ng-model='user.name']").send_keys('thj')
但是所需的元素是 Angular 元素,因此要将 字符序列 发送到您需要的元素为
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[name='name'][ng-model='user.name']"))).send_keys('thj')
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@name='name' and @ng-model='user.name']"))).send_keys('thj')
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC