首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Selenium在元素存在时找不到它吗?

Python Selenium在元素存在时找不到它吗?
EN

Stack Overflow用户
提问于 2021-07-29 10:32:01
回答 2查看 77关注 0票数 0

我正在尝试从一堆产品urls列表中抓取产品的交付日期数据。

我在终端上运行python文件进行多处理,所以最终这会打开多个chrome浏览器(比如10 - 15个),这会让我的电脑变慢很多。

我的代码基本上是单击包含发货选项的块,这将显示一个弹出框,其中显示预计的发货时间。我已经在下面的代码中包含了一个产品url的示例。

我注意到我的一些chrome浏览器冻结了,并且不能像我在代码中那样定位元素并单击它。我已经在我的代码中加入了在webdriver中刷新页面的功能,以防它能起到作用,但看起来冻结的浏览器甚至都不会刷新。

我不知道为什么它会这么做,因为我已经将webdriver设置为等待元素可点击。我是增加time.sleep()中的时间还是增加webdriverwait()中的秒数来解决这个问题?

代码语言:javascript
复制
chromedriver = "path to chromedriver"
driver = webdriver.Chrome(chromedriver, options=options)

# Example url
url = "https://shopee.sg/Perfect-Diary-X-Sanrio-MagicStay-Loose-Powder-Weightless-Soft-velvet-Blurring-Face-Powder-With-Cosmetic-Puff-Oil-Control-Smooth-Face-Powder-Waterproof-Applicable-To-Mask-Face-Cinnamoroll-Purin-Gudetama-i.240344695.5946255000?ads_keyword=makeup&adsid=1095331&campaignid=563217&position=1"

driver.get(url)
time.sleep(2)

try:
    WebDriverWait(driver,60).until(EC.element_to_be_clickable((By.XPATH,'//div[@class="flex flex-column"]//div[@class="shopee-drawer "]'))).click()
    while retries <= 5:
        try:
            shipping_block = WebDriverWait(driver,60).until(EC.element_to_be_clickable((By.XPATH,'//div[@class="flex flex-column"]//div[@class="shopee-drawer "]'))).click()
            break
        except TimeoutException:
            driver.refresh()
            retries += 1
except (NoSuchElementException, StaleElementReferenceException):
        delivery_date = None
EN

回答 2

Stack Overflow用户

发布于 2021-07-29 11:21:43

当你悬停鼠标时,你想要的元素将会显示,V元素的类型是svg,你需要相应地处理它。

代码语言:javascript
复制
you can take the help of this XPath to hover the mouse
((//*[name()='svg']//*[name()='g'])/*[name()='path'][starts-with(@d, 'm11 2.5c0 .1 0 .2-.1.3l')])

Getting the text from the popUp you need to check with this XPath
((//div[@class='shopee-drawer__contents']//descendant::div[4]))

您可以使用get_attribute("innerText")获取所有值

你可以在这里查看相同的answer,我希望它会有所帮助

票数 0
EN

Stack Overflow用户

发布于 2021-07-30 04:03:11

首先,不要使用webdriver.ChromeOption()中的headless选项来让网页窗口弹出,以观察元素是否被单击。

其次,您的代码只是单击元素,它不会帮助您获得任何东西。点击并打开新窗口后,它应该会这样做:

代码语言:javascript
复制
items = WebDriverWait(driver, 60).until(
            EC.visibility_of_all_elements_located((By.CLASS_NAME, 'load-done'))
        )
       
for item in items:
  deliver_date= item.get_attribute('text') 
  print(deliver_date)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68569354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档