NoSuchElementException in Selenium Python - python

I'm trying to open a website using Selenium, find a specific search box within it and fill it with a company name (MAGAZINE LUIZA S.A. in the example) and then click the "Search" button right next to the search box, since it will not work if I just hit "enter".
nav = webdriver.Edge()
nav.implicitly_wait(10)
nav.get('http://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/empresas-listadas.htm')
nav.find_element_by_xpath("/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[1]/label/span[1]/input[1]").send_keys('MAGAZINE LUIZA S.A.')
nav.find_elements_by_xpath('/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[2]/input').click()
And I get the Xpath of the search bar and search button by inspecting them and "copying full Xpath" in Microsoft Edge.
Problem is I get this error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[1]/label/span[1]/input[1]"}
(Session info: MicrosoftEdge=91.0.864.54)
But I have verified that I have copied and pasted the correct Xpaths for both the commands. Can anyone help?

You need to handle iframe, that XPath is relative to iframe.
This is your modified code
nav = webdriver.Edge()
nav.implicitly_wait(10)
nav.get('http://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/empresas-listadas.htm')
iframe = nav.find_element_by_xpath('/html/body/main/div[4]/div/div[1]/div[1]/div/form/section/div/div/div/iframe')
nav.switch_to.frame(iframe)
inp = nav.find_element_by_xpath("/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[1]/label/span[1]/input[1]")
inp.send_keys('MAGAZINE LUIZA S.A.')
nav.find_element_by_xpath('/html/body/form/div[3]/div[1]/div/div/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/div[2]/input').click()

Related

Need help finding the compose button in Gmail using selenium

I am having trouble with finding the compose button in Gmail. Every time I run it I get the following error message saying that the element doesn't exist
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#class="T-I T-I-KE L3"]"}
I have tried multiple things including:
ComposeButton = driver.find_element(By.XPATH, '//*[#jscontroller="eIu7Db"]')
ComposeButton = driver.find_element(By.XPATH, '//*[#class="T-I T-I-KE L3"]')
ComposeButton = driver.find_element(By.XPATH, '//*[#jsaction="click:dlrqf; clickmod:dlrqf"]')
They have all returned with the same error message
How do I access the compose button?
The greyed out part of the HTML in the linked image is for the compose button
Try this:
find_element(By.XPATH, "//div[#class='aic']/div/div")
This Worked for me
driver.find_element(By.CSS_SELECTOR, "[gh='cm']").click()
(N.B = change the word driver as per your code)

Selenium - Unable to locate element

I'm trying to web scrape the list of restaurants from a website using Selenium, but I always get the error NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//*[#id="lista-encontrada"]/div[2]/div[1]/h4"}
I've searched for similar errors but it's always related to frame or iframe, but I can't seem to find them in this particular website. Here's the code:
from selenium import webdriver
url = 'https://www.restaurantemadero.com.br/pt/restaurante/sp/sao-paulo'
driver = webdriver.Chrome()
driver.get(url)
driver.maximize_window()
restaurants = driver.find_elements_by_class_name('blocos')
nome_loja = restaurants[0].find_element_by_xpath('.//*[#id="lista-encontrada"]/div[7]/div[1]/h4')
Any hint will be very helpful!
To get the restaurant name use following xpath. //div[#class='blocos']//h4
Following code will returns restaurant name in a list
value using text:
print([res.text for res in driver.find_elements_by_xpath("//div[#class='blocos']//h4")])
OR
value using textContent:
print([res.get_attribute("textContent") for res in driver.find_elements_by_xpath("//div[#class='blocos']//h4")])

Click on link within another CSS wrapper - Selenuim Python

I am trying to click on the profile picture on top right to be able to logout of the website.
This is Cisco's license portal website "Logout" link; when you click on the profile picture, it drops down with a "Logout" link.
I can navigate through the body without any issues, but whenever I try to access the header of the site, it fails to find the element.
Using Selenium Chrome driver version 87 with Python
I can't seem to access that Profile Picture highlighted in red in order to logout with chrome selenium driver, it keeps telling me 'Element not found' using the ID or XPATH:
################ Log out ##############################
print('>>> Logging out of Cisco Cloud website')
logout = WebDriverWait(browser, element_timeout,ignored_exceptions=ignored_exceptions)\
.until(EC.presence_of_element_located((By.XPATH, '//*[#id="fwt-profile-button-loggedout"]')))
time.sleep(2)
logout = browser.find_element_by_xpath('//*[#id="fwt-profile-button-loggedout"]')
logout.click()
time.sleep(1)
logout1 = WebDriverWait(browser, element_timeout,ignored_exceptions=ignored_exceptions)\
.until(EC.presence_of_element_located((By.LINK_TEXT, 'Logout')))
time.sleep(2)
logout1 = browser.find_element_by_link_text('Logout')
logout1.click()
time.sleep(5)
------------------
I tried this based on this answer, but still cannot get it to find the element:
https://stackoverflow.com/a/48756722/12288943
def expand_shadow_element(element):
shadow_root = browser.execute_script('return arguments[0].shadowRoot', element)
return shadow_root
################# Log out ##############################
print('>>> Logging out of Cisco Cloud website')
logout1 = browser.find_element_by_xpath('//*[#id="overlayBlack"]/csc-header')
shadow_root1 = expand_shadow_element(logout1)
logout2 = browser.find_element_by_xpath('//*[#id="overlayBlack"]/csc-header//div')
shadow_root2 = expand_shadow_element(logout2)
logout = browser.find_element_by_id('fwt-profile-button-loggedout')
time.sleep(2)
logout.click()
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="overlayBlack"]/csc-header//div"}
(Session info: chrome=87.0.4280.88)
There is a #shadow-root(0) inside the 'csc-header' tag. The element you're trying access is inside shadow-root. I think that's why you are getting an 'Element not found' error. You have to navigate into shadow first. Hope this points you in the right direction.
<csc-header>
#shadow-root (open)
<svg id='fwt-profile-button-loggedout'></svg>
</csc-header>

Auto Login Python support needed

i am into my first-week learning python and am looking to automate login to a website. I can get to the website and get the username and password entered but the issue is the submit/login button. I have tried to use the same as the username and password using the below but it doesn't find this id, i am using the inspect tool on chrome to find the ids as i did for the username and password.
driver.find_element_by_id('GSubmitButtton-8f6df913-9588-4480-9944-538b785b8ece').click()
Error message
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="GSubmitButtton-8f6df913-9588-4480-9944-538b785b8ece"]"}
driver.find_element_by_id('GSubmitButtton-8f6df913-9588-4480-9944-538b785b8ece').click()
Login URL = https://www.gamma-portal.com/login.jspa
Would it be possible to use XPath to locate the login button?
You could find this by:
Right clicking
on the log in button,
Clicking inspect from the dropdown,
Right
click on the button element,
Select copy,
Copy XPath
Then enter the result into this line (replace the ###)
driver.find_element_by_xpath("###").click()
Some documentation for this method here!

Can not locate element inside the frame WHEN running all code

I want to locate the elements of a popup on some page,
the popup html is written in an iframe,
also the popup is triggered by clicking a link on the main page.
The weird thing is, if I run the whole code, I can not locate the 'target' element:
dr = webdriver.Chrome('chromedriver.exe', options=chrome_options)
modify = (By.CLASS_NAME, "modify")
ec_visible(dr, modify).click()
popup = (By.CLASS_NAME, "add-addr-iframe")
dr.switch_to.frame(ec_visible(dr, popup))
target = (By.CLASS_NAME, "cndzk-entrance-division-header-click")
ec_visible(dr, target).click()
def ec_visible(driver, locator):
return WebDriverWait(driver, 5).until(EC.visibility_of_element_located(locator))
But, if I first open the popup then locate, it works.
First:
modify = (By.CLASS_NAME, "modify")
ec_visible(dr, modify).click()
#popup = (By.CLASS_NAME, "add-addr-iframe")
#dr.switch_to.frame(ec_visible(dr, popup))
#target = (By.CLASS_NAME, "cndzk-entrance-division-header-click")
#ec_visible(dr, target).click()
Then: (works too if I manually open the popup and run this code)
#modify = (By.CLASS_NAME, "modify")
#ec_visible(dr, modify).click()
popup = (By.CLASS_NAME, "add-addr-iframe")
dr.switch_to.frame(ec_visible(dr, popup))
target = (By.CLASS_NAME, "cndzk-entrance-division-header-click")
ec_visible(dr, target).click()
Appreciate if you can point out my problem!
Here is the exception from shell:
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
This is the html screenshot,
sometimes the content in iframe can not even be seen.
iframe not extendable
when iframe extendable
7/26 update
I am wondering whether I asked the right question which may lead you guys just focusing on my code part. Since my code works(seperately), the elements and frames approach are good.
I step back and find one detail which may help but I don't know how it matters.
Here are the two shots about ctrl-F some element in the page source:
Normal result: target found and highlighted
Weird result: target found and no highlight
I mean when the page exists 'weird result', my code does not work.
PS. The page is the order-confirmation part of an e-commerical site, but the site groups its goods into two types which led to TWO types order page.
For ifarmes you have frame_to_be_available_and_switch_to_it as the EC.
So try this:
WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "add-addr-iframe")))
target = (By.CLASS_NAME, "span.cndzk-entrance-division-header-click")
ec_visible(dr, target).click()

Categories

Resources