如何使用 xpath 在 class 中查找值? (硒铬驱动程序)
How to find a value inside of a class using xpath? (Selenium ChromeDriver)
我正在尝试使用 ChromeDriver 从以下 html 代码中提取值“/mva/library/20120730/93135a040s.gif”:
目前,我的代码:
is_black_white = driver.find_elements_by_xpath("//div[@class='aj cw cy db ImgCaptionCntnrHover']/img[@data-filterwithidind='True']")
x = is_black_white[0].get_attribute("title src")
print(x)
正在返回“None”。
我觉得我很亲近。非常感谢任何帮助!
谢谢!
编辑:完整的 url 是 https://www.mcmaster.com/94735A701/
xpath 不正确。您可以尝试此解决方案。请注意,有一个双斜杠表示带有 img 标签的任何子节点。
WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='aj cw cy db ImgCaptionCntnrHover']//img")))
is_black_white = driver.find_elements_by_xpath("//div[@class='aj cw cy db ImgCaptionCntnrHover']//img")
print(len(is_black_white))
x = is_black_white[0].get_attribute("src")
print(x)
#Prints all the src urls
for ele in is_black_white:
print(ele.get_attribute("src"))
输出:
4
https://www.mcmaster.com/mva/library/20120730/93135a040s.gif
https://www.mcmaster.com/mva/library/20120730/93135a040s.gif
https://www.mcmaster.com/mva/library/20120730/94735a040s.gif
https://www.mcmaster.com/mva/library/20120730/93135a040s.gif
https://www.mcmaster.com/mva/library/20120730/94735a040s.gif
我正在尝试使用 ChromeDriver 从以下 html 代码中提取值“/mva/library/20120730/93135a040s.gif”:
目前,我的代码:
is_black_white = driver.find_elements_by_xpath("//div[@class='aj cw cy db ImgCaptionCntnrHover']/img[@data-filterwithidind='True']")
x = is_black_white[0].get_attribute("title src")
print(x)
正在返回“None”。
我觉得我很亲近。非常感谢任何帮助!
谢谢!
编辑:完整的 url 是 https://www.mcmaster.com/94735A701/
xpath 不正确。您可以尝试此解决方案。请注意,有一个双斜杠表示带有 img 标签的任何子节点。
WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='aj cw cy db ImgCaptionCntnrHover']//img")))
is_black_white = driver.find_elements_by_xpath("//div[@class='aj cw cy db ImgCaptionCntnrHover']//img")
print(len(is_black_white))
x = is_black_white[0].get_attribute("src")
print(x)
#Prints all the src urls
for ele in is_black_white:
print(ele.get_attribute("src"))
输出:
4
https://www.mcmaster.com/mva/library/20120730/93135a040s.gif
https://www.mcmaster.com/mva/library/20120730/93135a040s.gif
https://www.mcmaster.com/mva/library/20120730/94735a040s.gif
https://www.mcmaster.com/mva/library/20120730/93135a040s.gif
https://www.mcmaster.com/mva/library/20120730/94735a040s.gif