英文:
Cant select a <li element from a dropdon menu using the Selenium
问题
I've opened the menu using the provided code:
browser = webdriver.Chrome()
url="https://www.daraz.pk/products/candy-by-haier-1-ton-heat-cool-dc-inverter-white-colour-ac-csu-12hp10-years-brand-warranty-i276904502-s1494701276.html?spm=a2a0e.seller.list.2.18b049aahnIdR0&mp=1"
browser.get(url)
browser.maximize_window()
browser.find_element(By.CSS_SELECTOR,"#module_product_review > div > div > div:nth-child(2) > div > div:nth-child(3)").click()
However, when trying to click an option from the dropdown menu with this code:
browser.find_element(By.CSS_SELECTOR,'body > div:nth-child(49) > div > div > ul > li:nth-child(2)').click()
You're encountering the error:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"body > div:nth-child(49) > div > div"}
You've also tried various solutions from this question, but none seem to be working. You've tried using XPATH, CSS selector, LINK_NAME, and PARTIAL_LINK_NAME without success. Any help would be greatly appreciated.
英文:
I'm trying to click on the filter menu on an e-commerce page using selenium and then sort the reviews on the product page by recent.
I have managed opened the menu using the following code
browser = webdriver.Chrome()
url="https://www.daraz.pk/products/candy-by-haier-1-ton-heat-cool-dc-inverter-white-colour-ac-csu-12hp10-years-brand-warranty-i276904502-s1494701276.html?spm=a2a0e.seller.list.2.18b049aahnIdR0&mp=1"
browser.get(url)
browser.maximize_window()
browser.find_element(By.CSS_SELECTOR,"#module_product_review > div > div > div:nth-child(2) > div > div:nth-child(3)").click()
however when i try to click an option from the dropdown menu using the following code
browser.find_element(By.CSS_SELECTOR,'body > div:nth-child(49) > div > div > ul > li:nth-child(2)').click()
i keep getting the error
Message: no such element: Unable to locate element: {"method":"css selector","selector":"body > div:nth-child(49) > div > div"}
This is the HTML for the drop down-(Sorry, I'm new to coding and don't know how to copy the html and paste it here, attaching an image instead
Ive tried multiple solutions including the solution in this question
Nothing seems top be working and i cant figure why it wont detect an element on either XPATH or CSS detector. I've even tried using LINK_NAME and PARTIAL_LINK_NAME and i run into the same issue. Any help will be greatly appreciated.
Regards
答案1
得分: 0
你需要使用"WebDriverWait"来等待页面渲染完成。
尝试以下代码:
import time
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
url = "https://www.daraz.pk/products/candy-by-haier-1-ton-heat-cool-dc-inverter-white-colour-ac-csu-12hp10-years-brand-warranty-i276904502-s1494701276.html?spm=a2a0e.seller.list.2.18b049aahnIdR0&mp=1"
browser.get(url)
browser.maximize_window()
xpath = r'//*[@id="module_product_review"]/div/div/div[2]/div/div[2]'
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, xpath))).click()
time.sleep(10)
英文:
You have to use "WebDriverWait" to wait until the page is being rendered
try this code :
import time
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
url="https://www.daraz.pk/products/candy-by-haier-1-ton-heat-cool-dc-inverter-white-colour-ac-csu-12hp10-years-brand-warranty-i276904502-s1494701276.html?spm=a2a0e.seller.list.2.18b049aahnIdR0&mp=1"
browser.get(url)
browser.maximize_window()
xpath= r'//*[@id="module_product_review"]/div/div/div[2]/div/div[2]'
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, xpath))).click()
time.sleep(10)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论