How to translate selenium into python? - python

can someone translate this into python code so, for example, I know the first and second line will be
driver.get("link im using")
driver.set_window_size(945, 757)
I just can't figure out the rest

The equivalent python code for:
will be:
driver.get("link im using")
driver.set_window_size(945, 757)
driver.switch_to.frame(1)
driver.find_element_by_css_selector(".tg-join > span").click()
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
How can I select a html element no matter what frame it is in in selenium?
Switch to an iframe through Selenium and python

to select a frame you can use
driver.switch_to_frame()
to click on an item you can use
driver.find_element_by_xpath().click()
to switch to a window
driver.switch_to_window()

Related

Unable to switch to iFrame using Selenium

I'm currently trying out Selenium for Python to do some web automation.
I got stuck a the checkout where you have to enter your credit card data.
I'm aware that I have to switch to iFrame to locate the input box.
But I was never able to switch the frame.
I tried it for ID or for example for this:
time.sleep(4)
frame = self.driver.find_element_by_xpath("//iframe[#name= '__privateHeidelpayFrame--heidelpay-holder-iframe-1616489954834']")
self.driver.switch_to.frame(frame)
name_input = self.driver.find_element_by_id("card-holder")
name_input.clear()
name_input.send_keys(credit_card.NAME)
I'm always getting "Unable to locate element : {"method":"xpath","selector":"//iframe[#name= '__privateHeidelpayFrame--heidelpay-holder-iframe-1616489954834']"}".
I added a pic from the Source Code.
Source Code from the Webpage
I'm thankful for any advise!
Best, Jannic
Possibly id & name attribute is dynamic.
Try with starts-with() or use other attribute like class to identify the iframe.
frame = self.driver.find_element_by_xpath("//iframe[starts-with(#name, '__privateHeidelpayFrame--heidelpay-holder-iframe-')]")
self.driver.switch_to.frame(frame)
OR
frame = self.driver.find_element_by_xpath("//iframe[#class='heidelpayUIIframe']")
self.driver.switch_to.frame(frame)
Have you tried using a simpler API to find the iFrame?
Does
self.driver.find_element(By.NAME, "__privateHeidelpayFrame--heidelpay-holder-iframe-1616489954834'")
Return anything?

How to find href through class name and click with selenium

I'm using Selenium with python to make a spider.
A part of the web page is like this:
text - <span class="sr-keyword">name</span> text
I need to find the href and click.
I've tried as below:
target = driver.find_element_by_class_name('_j_search_link')
target.click()
target is not None but it doesn't seem that it could be clicked because after target.click(), nothing happened.
Also, I've read this question: Click on hyperlink using Selenium Webdriver
But it can't help me because in my case, there is a <span class>, not just a simple text Google.
You can look for an element with class _j_search_link that contains text Wat Chedi Luang
driver.find_element_by_xpath('//a[#class="_j_search_link" and contains(., "Wat Chedi Luang")]')
driver.find_elements_by_partial_link_text("Wat Chedi Luang").click()
I think you didn't find right element. Use CSS or Xpath to target this element then click it, like:
//a[#class='_j_search_link']
or
//a[#class='_j_search_link']/span[#class='sr-keyword']

How to identify and switch to the frame source in Selenium?

I have been using the Selenium and Python2.7, on Firefox.
I would like it to work like the following, but I do not know how to write code.
Driver.get('https://video-download.online')
Url='https://www.youtube.com/watch?v=waQlR8W8aTA'
Driver.find_element_by_id("link").send_keys(Url)
Driver.find_element_by_id("submit").click()
time.sleep(5)
#[Click]lowermost(highest quality) radio button
#[Click]Proceed button
You are able to get frame locator value in HTML code which is specified in iframes tag. u can move up from where your locator value is existed in HTML code.
This function is suitable:
def frame_switch(css_selector):
driver.switch_to.frame(driver.find_element_by_css_selector(css_selector))
If you are just trying to switch to the frame based on the name attribute, then you can use this:
def frame_switch(name):
driver.switch_to.frame(driver.find_element_by_name(name))

Extracting hidden element in Selenium

I have an element of type hidden in an iframe. I am wondering if there would be any way to get this value as I am using selenium. More specifically it is a captcha field. I've tried pulling it with something along the lines of
#!/usr/bin/env python
from selenium import webdriver
driver=webdriver.Chrome(chrome_bin_path)
driver.get('http://websitehere.com')
print driver.find_element_by_xpath('//*[#id="recaptcha-token"]').text
but because of it's hidden nature it returns nothing.
Below is a snippet of the source.
Highlighted is the string of interest. (value)
driver.switch_to_frame('undefined')
token_value = driver.find_element_by_id('recaptcha-token').get_attribute('value')
driver.switch_to_default_content()
Moving between windows and frames.
Use this method
hidden_text = element.get_attribute("textContent")

Selenium CSS_Selector looking for an element with specific text is turning out a 'NoSuchElementException'

I'm having an issue with looking for an element with a particular text attribute utilizing CSS_Selectors in Selenium. Here is the current line of code I have:
element = driver.find_element(By.CSS_SELECTOR, "li.adTypeItem[text='CLASS']")
I've had trouble using the attribute selector brackets in CSS_Selectors in the past, and clearing this up would really go a long way to better understanding how to use CSS_Selectors in the future.
Please note - i'm not looking for a element with a class, but rather the actual text that is displayed with that element.
Is this the only place on the page with the text "class" displayed? If so you can try:
driver.find_element(By.LINK_TEXT('CLASS'));
driver.find_element(By.PARTIAL_LINK_TEXT('CLASS'));

Categories

Resources