【问题标题】:Python selenium I can't acess to a linked page on a page with iframePython selenium 我无法访问带有 iframe 的页面上的链接页面
【发布时间】:2022-01-23 00:48:27
【问题描述】:

我想通过点击“Régional féminin U15”从这个页面转到这个https://resultats.ffbb.com/organisation/b5e6211d5970.html 到这个页面https://resultats.ffbb.com/championnat/b5e6211f621a.html?r=200000002810394&d=200000002911791&p=2。 我尝试了许多解决方案,但我拥有的最好的解决方案不是系统地工作。 请问你能帮帮我吗?

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

driver = webdriver.Firefox()
driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")

driver.switch_to.frame("idIframeChampionnat")
#sign_in = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/pre/span[93]'))).click();

button = driver.find_element_by_partial_link_text(u"minin U15")
button.click()```

【问题讨论】:

    标签: python selenium xpath iframe css-selectors


    【解决方案1】:

    试试

    driver.switch_to.frame("idIframeChampionnat")
    button = driver.find_element_by_xpath("//a[contains(text(), 'minin U15')]")
    button.click()
    

    driver.switch_to.frame("idIframeChampionnat")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'minin U15')]"))).click()
    

    这是我在 groovy 中的代码示例(抱歉,没有 python env),与您的非常相似,即使没有超时,它也可以在循环中完美运行 20 次。

        @Test(invocationCount = 20)
        void test() {
            driver.get('https://resultats.ffbb.com/organisation/b5e6211d5970.html')
            driver.switchTo().frame(
                    driver.findElement(By.xpath("//iframe[@id='idIframeChampionnat']"))
            )
            driver.findElement(By.xpath("//a[contains(text(), 'minin U15')]")).click()
            driver.switchTo().defaultContent()
            assert driver.getCurrentUrl() ==
                    'https://resultats.ffbb.com/championnat/b5e6211f621a.html?r=200000002810394&d=200000002911791&p=2'
        }
    

    所以,我没有想法。或许点击后添加driver.switch_to.default_content()

    【讨论】:

    • 感谢您的提议,我已经尝试了这两种解决方案,并且使用这两种解决方案我都停留在同一个起始页面上。
    • 修正它已经奏效(第二个解决方案),但在 10 次尝试中只有一次。
    • @Elmanu20,您能分享您的错误消息/堆栈跟踪吗?您可能会丰富您的问题。
    • 对不起,我没有任何踪迹。这段代码大多数时候不会产生任何可见的效果,有时它会起作用..
    • 我使用的是 chrome v96,它可以工作,但直到现在我在 Firefox 上,那里有随机行为。我还没有尝试重新安装我原来的网络浏览器。感谢您的帮助!
    【解决方案2】:

    链接上的click(),文本为Régional féminin U15,因为元素位于iframe 内,因此您必须:

    • 诱导WebDriverWait 使所需的帧可用并切换到它

    • 诱导WebDriverWait 使所需的元素可点击

    • 您可以使用以下任一Locator Strategies

      • 使用CSS_SELECTOR

        driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")
        WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#idIframeChampionnat")))
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Régional féminin U15"))).click()
        
      • 使用XPATH

        driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")
        WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='idIframeChampionnat']")))
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Régional féminin U15"))).click()
        
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
    • 浏览器快照:


    参考

    您可以在以下位置找到一些相关讨论:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 2012-08-15
      • 2012-05-18
      相关资源
      最近更新 更多