"Selenium中无法点击元素"

2024-10-02 20:44:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想刮:

https://shop.mango.com/us/women/jackets-biker-jackets/leather-biker-jacket_53095011.html?c=CG&n=1&s=prendas_she.familia;16,304

我的Selenium代码点击“发现我的尺寸”,然后进入身高和体重点击下一步按钮移动到下一页。然后点击腹部形状和臀部形状的框架出现。它需要单击相关的范围,然后移到下一步。我无法使用以下代码单击它:

if "straighter" in str(df_temp['hshape']):
                print("here hshapeeeeeeeeee")
                xpath="/html/body/div[9]/div[3]/div/div[2]/div[1]/div/div/div[3]/div/div/ul/li[1]/span/img"
                straighter = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, xpath))).click()

与此类似的是,我的代码单击肚皮形状,效果很好:

if "flatter" in str(df_temp['bshape']):
                xpath="/html/body/div[9]/div[3]/div/div[2]/div[1]/div/div/div[3]/div/div/ul/li[1]/span"
                flatter = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, xpath)))
                driver.execute_script("arguments[0].click();", flatter)

也不会引发任何异常。只有出口

编辑:演练:

enter image description here

enter image description here

enter image description here

enter image description here


Tags: 代码indivdfifhtmldriverxpath
1条回答
网友
1楼 · 发布于 2024-10-02 20:44:07

如果您想快速解决问题,请尝试:

xpath="//span/div[text()='Straighter']"

我建议使用xpath,它不依赖于确切的位置,而是通过特定的元素属性来标识其目标。在本例中,它将使用元素的文本。你知道吗

然而,文本并不总是一个可靠的因素,尤其是在多语言网站上。或者,您可以尝试以下方法:

xpath="//span[contains(@aria-label,'Hip Shape Selection button - Straighter')]"

相关问题 更多 >