【问题标题】:Is it possible to navigate to the 6th svg tag and then the 4th g tag to get the text of '35'?是否可以导航到第 6 个 svg 标签,然后是第 4 个 g 标签以获取“35”的文本?
【发布时间】:2021-10-12 15:09:21
【问题描述】:

The text '35' is what I am interested of capturing. (It is always the 6th svg tag followed by 4th g tag

My code:

driver = webdriver.Chrome()
driver.get(url)
value = driver.find_element_by_xpath("//*[name()='div' and @id='chartContainer']//*[name()= 'svg' and @style= 'position: absolute; z-index: 6;']//*[name()= 'g' and @font-size= '10pt]")
driver.quit()
print(value)

[网站网址][2]

错误信息: *

InvalidSelectorException:消息:无效选择器:无法定位 具有 xpath 表达式的元素 //[name()='div' 和 @id='chartContainer']//[name()= 'svg' 和@style= '位置: 绝对; z-index: 6;']//[name()= 'g' and @font-size= '10pt] 因为 以下错误: SyntaxError: Failed to execute 'evaluate' on 'Document':字符串 '//[name()='div' 和 @id='chartContainer']//[name()= 'svg' 和 @style= '位置: 绝对; z-index: 6;']//[name()= 'g' and @font-size= '10pt]' 不是 一个有效的 XPath 表达式。 (会话信息:chrome=94.0.4606.81)

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    你可以像这样指向第 6 个 svg 和第 4 个 g 标签:

    ((//div[@id='chartContainer']//*[name()='svg'])[6]//*[name()='g'])[4]//*[name()='text']
    

    在代码中:

    value = driver.find_element_by_xpath("((//div[@id='chartContainer']//*[name()='svg'])[6]//*[name()='g'])[4]//*[name()='text']").text
    print(value)
    

    请在交互之前进行一些明确的等待。

    代码试用 1:

    time.sleep(5)
    value = driver.find_element_by_xpath("((//div[@id='chartContainer']//*[name()='svg'])[6]//*[name()='g'])[4]//*[name()='text']").text
    print(value)
    

    代码试用 2:

    value = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "((//div[@id='chartContainer']//*[name()='svg'])[6]//*[name()='g'])[4]//*[name()='text']"))).text
    print(value)
    

    进口:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

    PS:如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

    检查步骤:

    Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到突出显示 987654333@匹配节点。

    【讨论】:

    • 你好 Cruisepandey。 TQVM 的帮助! from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC value = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "((//div[@id='chartContainer']//*[name()='svg'])[6]//*[name()='g'])[4]//*[name()='text']"))).text print(value) 此代码有效
    • 带有 time.sleep 的代码试用 1 也可以,但是,需要将其更改为 time.sleep(2)。正如我所测试的,time.sleep(5) 会给出一个错误,我认为这是由打印屏幕自动弹出引起的。
    • time.sleep() 不鼓励实践,请使用 Webdriverwait,我的解决方案中的代码试用 2。
    【解决方案2】:

    ' 在您的 xPath 表达式的最后一个缺失。

    value = driver.find_element_by_xpath("//*[name()='div' and @id='chartContainer']//*[name()= 'svg' and @style= 'position: absolute; z-index: 6;']//*[name()= 'g' and @font-size= '10pt']")
    

    但是上面xPath你写的内容指向第6个svg标签和第6个g标签。

    您需要使用以下任一xPath 来指向第6 个svg 和第4 个g 标记。

    //*[name()='div' and @id='chartContainer']//*[name()= 'svg' and @style= 'position: absolute; z-index: 6;']//*[name()= 'g' ][4]
    

    或者

    //*[name()='div' and @id='chartContainer']//*[name()= 'svg'][6]//*[name()= 'g' ][4]
    

    【讨论】:

    • 南丹您好,感谢您的帮助!该代码有效,但是,我们需要在此之前添加 time.sleep(2)。我试过time.sleep(5),它说找不到元素,我相信这是由于自动弹出的打印屏幕。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    相关资源
    最近更新 更多