【问题标题】:Why am I unable to select the following values from the dynamic drop down list?为什么我无法从动态下拉列表中选择以下值?
【发布时间】:2023-01-30 16:32:00
【问题描述】:
from selenium import webdriver  
import time  
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.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.remote.webelement import WebElement

driver = webdriver.Chrome('chromedriver')
driver.get('https://devbusiness.tunai.io/login')
time.sleep(2)
driver.maximize_window()

# Create variables for login credentials.
username = "your username"
password = "your password"

username = driver.find_element(By.NAME, "loginUsername");
username.send_keys("kevin@tunai");

password = driver.find_element(By.NAME, "loginPassword");
password.send_keys("xxxxx");

login = driver.find_element(By.XPATH,"//*[@id='app']/div/div/div/div/div/div[2]/form/div[4]/button");
login.submit();
time.sleep(2)

# Wait for login process to complete. 
WebDriverWait(driver=driver, timeout=10).until(
    lambda x: x.execute_script("return document.readyState === 'complete'")
)
# Verify that the login was successful.
error_message = "Incorrect username or password."
# Retrieve any errors found. 
errors = driver.find_elements(By.CLASS_NAME, "flash-error")

# When errors are found, the login will fail. 
if any(error_message in e.text for e in errors): 
    print("[!] Login failed")
else:
    print("[+] Login successful")

driver.get("https://devbusiness.tunai.io/dashboard/salon_menu_service")

service = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/button")
service.click();
driver.find_element(By.TAG_NAME,"input").send_keys("Hair Dying")

price = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[2]/div[1]/div/div/input")
price.clear()
price.send_keys("50")

baseprice = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[2]/div[2]/div/div/input")
baseprice.clear()
baseprice.send_keys("10")

category = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[3]/div/div/div/div[2]")
Select.select_by_visible_text("Category 2 - BeautyPOS")
time.sleep(3)

我想从下拉列表中选择一个值,但是当我使用“选择”功能时它不起作用。我无法弄清楚问题是什么。已经尝试了很多方法来调试它但都失败了...如果有人可以提供帮助,请提前致谢。

【问题讨论】:

  • 你能提供你的整个代码和你想要抓取的网站吗?
  • 这不起作用的原因是因为Select only works on <select> elements。尝试将动态选择的html中路径div.multiselect__content-wrapper ul.multiselect___content li.multiselect__element span中span的类名multiselect__option更改为multiselect__option--selected
  • class="多选选项--已选择"。你是这个意思吗?它不起作用。
  • 抱歉,我没有想法,我不知道 vue.js。
  • 你能解释一下你想在沙龙服务菜单中选择什么吗?您提供的 xpath 没有在页面上找到任何元素。

标签: python selenium web-scraping


【解决方案1】:

我想我找到了一种方法如何在您的样式列表中选择类别。

from selenium import webdriver  
import time  
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.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.remote.webelement import WebElement

driver = webdriver.Chrome('chromedriver')
driver.get('https://devbusiness.tunai.io/login')
time.sleep(2)
driver.maximize_window()

# Create variables for login credentials.

username = driver.find_element(By.NAME, "loginUsername");
username.send_keys("kevin@tunai");

password = driver.find_element(By.NAME, "loginPassword");
password.send_keys("123456");

login = driver.find_element(By.XPATH,"//*[@id='app']/div/div/div/div/div/div[2]/form/div[4]/button");
login.submit();
time.sleep(2)

# Wait for login process to complete. 
WebDriverWait(driver=driver, timeout=10).until(
    lambda x: x.execute_script("return document.readyState === 'complete'")
)
# Verify that the login was successful.
error_message = "Incorrect username or password."
# Retrieve any errors found. 
errors = driver.find_elements(By.CLASS_NAME, "flash-error")

# When errors are found, the login will fail. 
if any(error_message in e.text for e in errors): 
    print("[!] Login failed")
else:
    print("[+] Login successful")

driver.get("https://devbusiness.tunai.io/dashboard/salon_menu_service")

service = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/button")
service.click();
driver.find_element(By.TAG_NAME,"input").send_keys("Hair Dying")

price = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[2]/div[1]/div/div/input")
price.clear()
price.send_keys("50")

baseprice = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[2]/div[2]/div/div/input")
baseprice.clear()
baseprice.send_keys("10")

# Category button
category_button = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[3]/div/div/div/div[2]")
# An "Category 2 - BeautyPOS" item in list of categories
category_select = driver.find_element(By.XPATH,"""//*[@id="page-content"]/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[3]/div/div/div/div[3]/ul/li[2]/span""")

# Click category button to show list.
category_button.click()
# Click on category you want select.
category_select.click()

time.sleep(3)

【讨论】:

  • 赞赏兄弟!现在可以用了!
猜你喜欢
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多