Python、Selenium...找不到明显存在的元素
Python, Selenium... not able to find an element which is obviously there
我正在尝试使用 Python (Selenium) 从此站点提取数据:
https://sin.clarksons.net/
我输入用户名和密码后,无法点击明显的"Submit"底部。你们中的一些人可以帮忙看看为什么吗? TIA.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
if __name__ == "__main__":
try:
chrome_path = r"C:\Users\xxx\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.clarksons.net/")
driver.maximize_window()
time.sleep(5)
login = driver.find_element_by_xpath('//*[@id="menu"]/li[1]/span')
time.sleep(5)
login.click()
time.sleep(5)
username = driver.find_element_by_xpath('//input[@id="usernameText"]')
username.clear()
username.send_keys("abc(at)hotmail.com")
password = driver.find_element_by_xpath('/html/body/div[6]/div/div/div[2]/form/div[2]/div/input[1]')
password.clear()
password.send_keys("xyzabc")
submit = driver.find_element_by_xpath('/html/body/div[6]/div/div/div[2]/form/div[4]/div/div/button')
submit.click()
time.sleep(5)
print "login"
driver.quit()
except Exception as e:
print e
driver.quit()
您可以通过标题找到登录按钮:
submit = driver.find_element_by_xpath('//button[contains(@title, "Login")]')
submit.click()
或者您可以找到表格然后提交(根据class查找):
submit_form = driver.find_element_by_xpath('//form[starts-with(@class, "ng-valid ng-dirty")]')
submit_form .submit()
希望对您有所帮助。
你用的xpath
错了,正确的是
'/html/body/div[6]/div/div/div[2]/form/div[4]/button'
顺便说一句,你真的不应该使用绝对 xpath
,例如你可以使用 '//button[@title="Login"]'
作为登录按钮。
尝试将 xpath 替换为 id 并使用 css 选择器作为登录按钮
username = driver.find_element_by_id("usernameText")
username.clear()
username.send_keys("vivi.sandsten@gard.no")
password = driver.find_element_by_id("passwordText")
password.clear()
password.send_keys("xyzabc")
#submit = driver.find_element_by_xpath(".//button[@title='Login']")
submit = driver.find_element_by_css_selector("#home button.btn-
primary")
submit.click()
我正在尝试使用 Python (Selenium) 从此站点提取数据: https://sin.clarksons.net/
我输入用户名和密码后,无法点击明显的"Submit"底部。你们中的一些人可以帮忙看看为什么吗? TIA.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
if __name__ == "__main__":
try:
chrome_path = r"C:\Users\xxx\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.clarksons.net/")
driver.maximize_window()
time.sleep(5)
login = driver.find_element_by_xpath('//*[@id="menu"]/li[1]/span')
time.sleep(5)
login.click()
time.sleep(5)
username = driver.find_element_by_xpath('//input[@id="usernameText"]')
username.clear()
username.send_keys("abc(at)hotmail.com")
password = driver.find_element_by_xpath('/html/body/div[6]/div/div/div[2]/form/div[2]/div/input[1]')
password.clear()
password.send_keys("xyzabc")
submit = driver.find_element_by_xpath('/html/body/div[6]/div/div/div[2]/form/div[4]/div/div/button')
submit.click()
time.sleep(5)
print "login"
driver.quit()
except Exception as e:
print e
driver.quit()
您可以通过标题找到登录按钮:
submit = driver.find_element_by_xpath('//button[contains(@title, "Login")]')
submit.click()
或者您可以找到表格然后提交(根据class查找):
submit_form = driver.find_element_by_xpath('//form[starts-with(@class, "ng-valid ng-dirty")]')
submit_form .submit()
希望对您有所帮助。
你用的xpath
错了,正确的是
'/html/body/div[6]/div/div/div[2]/form/div[4]/button'
顺便说一句,你真的不应该使用绝对 xpath
,例如你可以使用 '//button[@title="Login"]'
作为登录按钮。
尝试将 xpath 替换为 id 并使用 css 选择器作为登录按钮
username = driver.find_element_by_id("usernameText")
username.clear()
username.send_keys("vivi.sandsten@gard.no")
password = driver.find_element_by_id("passwordText")
password.clear()
password.send_keys("xyzabc")
#submit = driver.find_element_by_xpath(".//button[@title='Login']")
submit = driver.find_element_by_css_selector("#home button.btn-
primary")
submit.click()