【问题标题】:How to get next sibling xpath using selenium?如何使用硒获得下一个兄弟xpath?
【发布时间】:2018-10-16 09:39:45
【问题描述】:

我在尝试下载 csv 文件的地方有以下代码。

chromedriver = "/usr/lib/chromium-browser/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get('https://www.aemo.com.au/Electricity/National-Electricity-Market-NEM/Data-dashboard#aggregated-data')
time.sleep(5)
driver.find_element_by_xpath("//div[@class='dashboard-tab-content-options-state']//div[@class='dashboard-tab-content-options-item' and text()='SA']").click()
button = driver.find_element_by_xpath("//div[@class='dashboard-tab-content-download-btn' and text()= 'Download Historic Data as .csv']")
button.click() 

选择SA在第一个xpath中归档如下,

但是我想从下面的选项中选择 SA 字段,如下所示。

html代码如下:-

<div id="dashADF" class="dashboard-tab-content" style="display: block;">
            <div class="dashboard-tab-content-row">
    <div class="dashboard-tab-content-options">
        <div class="dashboard-tab-content-text-container">
            <div class="dashboard-tab-content-title">Aggregated Price and Demand Data - Current Month</div>
        </div>
        <div class="dashboard-tab-content-options-state">
            <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-options-item active">QLD</div>
            <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-options-item">NSW</div>
            <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-options-item">VIC</div>
            <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-options-item">TAS</div>
            <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-options-item">SA</div>
        </div>
    </div>
</div>
<div class="dashboard-tab-content-row">
    <div class="dashboard-tab-content-download">
        <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-download-btn">Download Current Month</div>
    </div>
</div><hr>
<div class="dashboard-tab-content-row">
    <div class="dashboard-tab-content-options">
        <div class="dashboard-tab-content-text-container">
        </div>
        <div>
            <div class="dashboard-tab-content-options-state">
                <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-options-item active">QLD</div>
                <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-options-item">NSW</div>
                <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-options-item">VIC</div>
                <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-options-item">TAS</div>
                <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-options-item">SA</div>
            </div>
            <div class="dashboard-tab-content-options-time">
            </div>
        </div>
    </div>
</div>
<div class="dashboard-tab-content-row">
    <div class="dashboard-tab-content-download">
        <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-download-btn">Download Historic Data as .csv</div>
    </div>
</div>
        </div>

【问题讨论】:

标签: python-3.x selenium selenium-webdriver xpath webdriver


【解决方案1】:

我会使用这个 XPath

//div[.='Aggregated Price and Demand Data - Historical']/following::div[.='SA']

这会让你得到你想要的。您甚至可以将其放入一个函数中,并传入部分名称和按钮文本并获得您想要的任何组合。

def click_button_in_section(section_name, button_text)
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[.='{}']/following::div[.='{}']".format(section_name, button_text)))).click()

然后像这样称呼它

click_button_in_section("Aggregated Price and Demand Data - Historical", "SA")
click_button_in_section("Aggregated Price and Demand Data - Historical", "NSW")
click_button_in_section("Aggregated Price and Demand Data - Current Month", "TAS")
click_button_in_section("Aggregated Price and Demand Data - Current Month", "VIC")

...等等

【讨论】:

    【解决方案2】:

    综合价格和需求数据 - 历史部分的SA 选项卡上的click(),您可以使用以下解决方案:

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      options = Options()
      options.add_argument("start-maximized")
      driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get('https://www.aemo.com.au/Electricity/National-Electricity-Market-NEM/Data-dashboard#aggregated-data')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='dashboard-tab-content' and @id='dashADF']//following::div[@class='dashboard-tab-content-options-item' and text()='SA'][2]"))).click()
      
    • 浏览器快照:

    【讨论】:

    • 使用索引是危险的,因为如果页面发生变化,您的定位器可能会继续工作,但点击了错误的项目......这意味着您没有测试预期的目标。这可能会导致难以追踪失败或通过,而不是测试您的意图......这两者都不好(tm)。
    【解决方案3】:

    两个元素看起来相同,标签内包含相同的文本, 我希望这会起作用,试试这个,

    driver.find_element_by_xpath("//div[@data-tabname='dashADF' and @data-current='false' and  text()='SA']");
    
    driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='TAS'])[2]/following::div[1]");
    
    driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Download Historic Data as .csv'])[1]/preceding::div[2]");
    
    driver.find_element_by_xpath("//div[@id='dashADF']/div[3]/div/div[2]/div/div[5]");
    
    driver.find_element_by_xpath("//*[@id='dashADF']/div[3]/div/div[2]/div[1]/div[5]");
    

    【讨论】:

    • 使用索引是危险的,因为如果页面发生变化,您的定位器可能会继续工作,但点击了错误的项目......这意味着您没有测试预期的目标。这可能会导致难以追踪失败或通过,而不是测试您的意图......这两者都不好(tm)。
    • @JeffC 你能修改我的答案,这样我就可以了解更多。
    • 当您使用包含div[3] 索引的定位器时,HTML 可能会发生变化,但您的定位器仍会查看第三个 DIV,这意味着它不再查看您想要的内容。所以要么你的定位器坏了,要么你可能点击了错误的按钮,这将很难追踪......它甚至可能不会失败。请参阅我的答案以获取不使用索引并将随标题文本移动的替代方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 2015-08-05
    • 2013-02-24
    • 2015-01-24
    • 1970-01-01
    相关资源
    最近更新 更多