Python, Selenium Webdriver: Spans are not loaded for Firefox driver - python

I have a problem with Firefox driver to display spans for one of the popups of my site.
I am not allowed to post images because of the reputation but here is the example how it looks like for Firefox and Chrome drivers:
https://www.dropbox.com/s/6yzgi40xljpdyp5/spans.png
So for Firefox I need to modify my locator:
_apply_button = (By.XPATH, "//button[contains(text(), 'Apply')]")
It does not work in this way for Chrome so there is another version for Chromedriver:
_apply_button = (By.XPATH, "//button[contains(span, 'Apply')]")
Is there a way to get the spans loaded properly?
Any help will be appreciated.

Try to select the element by link text:
_apply_button = driver.find_element_by_link_text('Apply')
An other solution is the following:
_apply_button = (By.XPATH, "//button/span[contains(text(), 'Apply')]")

Related

How to pass the RGPD popup / iframe with Selenium (Python)?

Ok, attempt to use Selenium on a website : lefigaro.fr, but no class related to the RGPD popup to be found by Selenium, even after a switch to frame. :/
I'm juste looking a reliable way to close it.
It goes this way :
from selenium import webdriver
WINDOW_SIZE = "1920,1080"
ADRESSE = 'https://www.lefigaro.fr/'
driver = webdriver.Firefox() #the chrome version was even worse, geckodriver a the root
driver.get(ADRESSE)
elt = driver.find_element_by_class_name("sc-18sn7k8-1") #error
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
elt = driver.find_element_by_class_name("sc-18sn7k8-1") #error
You can switch to iframe via index.
It seems that popup is the only iframe on the page, so perhaps this will work:
driver.switch_to.frame(0) # or perhaps (1)
EDIT:
It seems the page already is in an iframe when it loads.
Do driver.switch_to.default_content() first.
Then it seems the iframe you want is (3):
driver.switch_to.frame(3)
Hope this works.
Hmm, actually, not quite... I'll spend some more time on this.

Python Selenium "Unable to locate element"

I am trying to use Selenium to sign up an email account automatically whenever I need to. It's just a fun learning project for me. For the life of me I don't understand why it can't find the element. This code works fine on the sign-in page but not the sign-up page. I have tried all different Selenium commands and even tried using the ID and class name. Either is says it can't locate the element or that it is not reachable by keyboard.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
options = Options()
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe')
driver.get("https://mail.protonmail.com/create/new?language=en")
time.sleep(10)
username_input = driver.find_element_by_id("username").send_keys("testusername")
Also here is the HTML code: https://i.imgur.com/ZaBMTzG.png
The username field is in iframe, you need to switch to iframe to make this work.
Below is the code that works fine :
driver.get("https://mail.protonmail.com/create/new?language=en")
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='Registration form'][class='top']"))
driver.find_element_by_id("username").send_keys("some string")
read more about iframe here
learn more about how to switch to iframe/frame/framset using Python
selenium Bindings here
Update :
wait = WebDriverWait(driver, 30)
driver.get("https://mail.protonmail.com/create/new?language=en")
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='Registration form'][class='top']"))
driver.find_element_by_id("username").send_keys("some string")
driver.switch_to.default_content()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(5)
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='Registration form'][class='bottom']"))
wait.until(EC.element_to_be_clickable((By.NAME, "submitBtn"))).click()
I'm not sure if I've seen enough code to diagnose, but I think the way you are defining username_input seems problematic. driver.find_element_by_id("username").send_keys("testusername") doesn't actually return anything so it seems like you are setting username_input = null.

Is there a way using python to select an item in the combobox "Region" using selenium?

I want to select the first item of the combobox "Region" with this script in this website LINK Python + Selenium
chromeOptions = webdriver.ChromeOptions()
prefs = {'profile.managed_default_content_settings.images':2, 'disk-cache-size': 4096 }
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)
url = 'https://www.tgr.cl/certificado-pago-deudas-contribuciones-tramite/'
driver.get(url)
driver.switch_to.frame(driver.find_element_by_name('busqueda'))
driver.find_element_by_xpath("//select[#name='region']/option[text()='REGION DE ANTOFAGASTA']").click()
But im getting this Error when Im switching the frame. Output:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="busqueda"]"}
Any ideas why is this happening?
All subdocuments like the <iframe> your element is in lazyload into existence slower than your entire webpage. It might be best to wait a few seconds, or until Selenium detects the <iframe>.
# Wait specifically for that iframe, then switch to it
from selenium.webdriver.support.ui import WebDriverWait
iframe_detector = WebDriverWait(driver, 5).until(lambda x: x.find_element_by_name('busqueda'),
message="Iframe never loaded!")
driver.switch_to.frame(iframe_detector)
#Just wait and hope it loads in
driver.implicitly_wait(5)
driver.switch_to.frame(driver.find_element_by_name('busqueda'))
You may need longer wait times to account for your internet connection.
The problem was that the combobox is the iframe of an iframe. So you needed to switch to the first iframe and after to the his child in that way you will be OK.

Element not Clickable ( Chrome + Selenium + Python)

I am using Chromewebdriver /Selenium in Python
I tried several solutions ( actions, maximize window etc) to get rid of this exception without success.
The error is :
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (410, 513). Other element would receive the click: ...
The code :
from selenium import webdriver
import time
url = 'https://www.tmdn.org/tmview/welcome#/tmview/detail/EM500000018203824'
driver = webdriver.Chrome(executable_path = "D:\Python\chromedriver.exe")
driver.get(url)
time.sleep(30)
driver.find_element_by_link_text('Show more').click()
I tested this code on my linux pc with latest libraries, python3 and chromedriver. It works perfectly(to my mind). So try to update everything and try again(Try to not to leave chrome). Here is the code:
from selenium import webdriver
import time
url = 'https://www.tmdn.org/tmview/welcome#/tmview/detail/EM500000018203824'
driver = webdriver.Chrome(executable_path = "chromedriver")
driver.get(url)
time.sleep(30)
driver.find_element_by_link_text('Show more').click()
P.S. chromedriver is in the same folder as script.
Thank you for your assistance.
Actually, the issue was the panel on the footer of the web page 'We use cookies....' which is overlapping with the 'Show more' link, when the driver tries to click, the click was intercepted by that panel.
The solution is to close that panel, and the code worked fine.
code is working fine but if you manually click on some other element after page loaded before sleep time is over then you can recreate same error
for example after site is loaded and i clicked on element search for trade mark with similar image then selenium is not able to find search element.so maybe some other part of your code is clicking on other element and loading different url due to which
selenium is generating this error .your code is fine just check for conflict cases.

How to close pop up window in Selenium

Can't close pop up window which appears right after http://www.cargo.lt/ loads. Here's what I've got:
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get('http://www.cargo.lt/asp/index.asp?')
time.sleep(10)
driver.find_element_by_xpath('/html/body/div[36]/div/a').click()
I'm not very familiar with how to write custom xpath/css path and now just clicked on Inspect element and copied xpath. What I'm doing wrong?
EDIT:
What a stupid mistake. Didn't realize that when element is off screen Selenium can't click on it. Just added driver.maximize_window() and all my problems are gone. Thanks all for your answers. Unfortunately I can't vote yet, because I don't have enough points...
hi to close pop up /alert please use
driver.switch_to_alert()
then use
driver.find_element_by_xpath('/html/body/div[36]/div/a').click()
// if u have copied pasted form firebug then it will be correct i guess
or if ur xpath is not correct then use
driver.find_element_by_xpath("//a[#id='advert_x']").click()
i tried your code it's work fine i don't know whats you problem but
try this : driver.implicitly_wait(10)
insted of :
time.sleep(10)
You can use the Predefined commands in python -Selenium to switch to alert box.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.cargo.lt/asp/index.asp?')
alrt = driver.switch_to_alert()
alrt.accept()
Hope it Helps.
you can use chrome options
in Python, you can use
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-notifications")
webdriver.Chrome(os.path.join(path, 'chromedriver'),chrome_options=chrome_options)
in java
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
options.addArguments("test-type");
ChromeDriver driver = new ChromeDriver(options);

Categories