Multiple iframe tags Selenium webdriver

微笑、不失礼 提交于 2020-01-24 22:21:28

问题


I am trying to use selenium to send credit card info onto a site, and each element is contained in a separate iframe tag in the HTML. For example, the box to enter a credit card number is contained in the first iframe tag, and the card holder name is contained in the second iframe, and so on, and so forth. I am able to access the first iframe tag, and send the credit card number into the box, however I an unable to locate the cardholder name iframe tag, and thus cannot send the keys to enter a cardholder name. I'm wondering if there is a way to specifically search for the second (or an arbitrary number other than the first element) iframe tag with webdriver.

Here is the code I used:

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
cc = driver.find_element_by_id("number")
cc.send_keys(credit_card_number)

This correctly found the first iframe tag which contains the field to enter a card number, and sent the appropriate keys

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_id("name").send_keys(cc_name)

This resulted in the following error: Message: no such element: Unable to locate element:{"method":"tag name","selector":"iframe"}


回答1:


You have correctly found the first iframe tag which contains the credit card number field to enter the card number and sent the appropriate keys. Moving forward when you want to sent the appropriate keys to the card holder name field you need to be on the relevant frame first. So you have to switch back to the immedediate parent_frame() which contains both the iframe tags i.e. first iframe and second iframe and next try to switch to the second iframe to locate the card holder name field as follows :

# as there are multiple frames so find_element_by_tag_name("iframe") will not suffice and have to use unique css/xpath
driver.switch_to.frame(driver.find_element_by_xpath("xpath_of_iframe1"))
cc = driver.find_element_by_id("number")
cc.send_keys(credit_card_number)
driver.switch_to.default_content() #incase iframe1 and iframe2 are immediate child of the Top Level Content
driver.switch_to.frame(driver.find_element_by_xpath("xpath_of_iframe2"))
cc = driver.find_element_by_id("name")
cc.send_keys(cc_name)

Note : As per best practices you should not detect frames through find_element_by_tag_name("iframe") inpresence of multiple <iframe> tags on the webpage. The best way would be to use either Frame Name , Frame ID or Frame Index. You can find the detailed discussion in How can I select a html element no matter what frame it is in in selenium?.




回答2:


After switching to first iframe , you have to switch back to default context (your main page containing multiple iframes) & then switch to next iframe .

So your pseudo code should look like

driver.switch_to.frame(driver.find_element_by_tag_name("iframe_1"))
driver.switch_to.defaultContent()
driver.switch_to.frame(driver.find_element_by_tag_name("iframe_2"))
driver.switch_to.defaultContent()
& so on


来源:https://stackoverflow.com/questions/48533101/multiple-iframe-tags-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!