I would like to write a python code where he clicks on the href link.
Screenshot of HTML:
This is what I am currently having in Python, but it does not work.
tables = browser.find_elements_by_xpath('//*[#id="notice"]')
for table in tables:
row = table.find_element_by_xpath('//tr[#class="Zebra"]//td//a[#href="https://enot.publicprocurement.be/enot-war/preViewNotice.do?noticeId=438868&saveSearchParams=true&pageSize=%31%32%35&d-446978-p=%31&"]').click()
This is the error message for row:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//tr[#class="Zebra"]//td//a[#href="https://enot.publicprocurement.be/enot-war/preViewNotice.do?noticeId=438868&saveSearchParams=true&pageSize=%31%32%35&d-446978-p=%31&"]"}
Could you please tell me what I am doing wrong?
The desired element is a <a> element with the innerText as Universiteit Antwerpen-22004
To click on the desired element you can use either of the following locator strategies:
Using the href attribute:
table.find_element_by_xpath('.//tr[#class="Zebra"]//td//a[starts-with(#href, "https://enot.publicprocurement.be/enot-war/preViewNotice.do?noticeId")]').click()
Using the innerText:
table.find_element_by_xpath('.//tr[#class="Zebra"]//td//a[contains(., "Universiteit Antwerpen-22004")]').click()
Related
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()
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")])
I'm Trying to fill the next txtBox but I'have tried with the xpath, name, class.
<input name="txtNumDoc" type="text" id="txtNumDoc" class="txtBox">
But always get me the same error.
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(.,'txtNumDoc')]"}
First, the element you are trying to access is inside an iframe so you need to switch to that first:
iframe = driver.find_element_by_id("iframeBDUA")
driver.switch_to.frame(iframe)
and then you can find your element:
element = driver.find_element_by_id("txtNumDoc")
//*[contains(.,'txtNumDoc')] is not a valid xpath for this element as we don't have text txtNumDoc for this element. txtNumDoc is attribute value for both name and id in this case.
use the below.
//input[#id="txtNumDoc"]
I would like to be able to visit a list of hrefs and in each link, find a Webelement associated with a CSS selector:
elements = driver.find_elements_by_xpath("//article/a[#class='card-info']")
links = []
for elem in elements:
links.append(elem.get_attribute('href'))
links
This gets me the list of hrefs fine. I then try to loop through the hrefs by clicking on each link, finding the Webelement by CSS selector and appending it to an empty list:
for link in elements:
link.click()
year = []
year.append(driver.find_element_by_class_name('year-info'))
This does not work and I receive the following error: "NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".year-info"}"
I've only been writing in Python for a week so I apologize for any glaring syntax errors. I'd appreciate any guidance
This is my HTML code:
<button class="_qv64e _gexxb _r9b8f _njrw0">Follow</button>
But, when I try to click on this this error is occurred:
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: span._qv64e _gexxb _r9b8f _njrw0
It is happened in any way that I used, for example in xpath, CSS selector, tag name and ... . This error has been occurred through this code:
driver.find_element_by_css_selector("span._qv64e _gexxb _r9b8f _njrw0").click()
When using css_selector spaces has meaning. The selector you are using tell the driver to look for an element with <_njrw0> tag which has an ancestor with <_r9b8f> tag as so on. The button also has <button> tag, not <span> tag.
You need to use . in front of any class name and without spaces
driver.find_element_by_css_selector("button._qv64e._gexxb._r9b8f._njrw0").click()