无法使用Selenium选择下拉菜单中的<li元素。

huangapple go评论63阅读模式
英文:

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&amp;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=&quot;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&amp;mp=1&quot;
browser.get(url)
browser.maximize_window()


browser.find_element(By.CSS_SELECTOR,&quot;#module_product_review &gt; div &gt; div &gt; div:nth-child(2) &gt; div &gt; div:nth-child(3)&quot;).click()


however when i try to click an option from the dropdown menu using the following code

browser.find_element(By.CSS_SELECTOR,&#39;body &gt; div:nth-child(49) &gt; div &gt; div &gt; ul &gt; li:nth-child(2)&#39;).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

HTML Snippet

Ive tried multiple solutions including the solution in this question

https://stackoverflow.com/questions/40097838/how-to-click-on-the-list-of-the-li-elements-in-an-ul-elements-with-selenium

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&amp;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=&quot;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&amp;mp=1&quot;
browser.get(url)
browser.maximize_window()


xpath= r&#39;//*[@id=&quot;module_product_review&quot;]/div/div/div[2]/div/div[2]&#39;
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, xpath))).click()

time.sleep(10)

huangapple
  • 本文由 发表于 2023年5月22日 04:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定