I'm trying to click a the element with text as I don't have the telephone on this website.
So I find the element with inspect. here is the element in html:
<span class="toggle-link link_has-no-phone" role="button">I don't have a telephone number</span>
In my nonfunctional code i wrote this:
r = driver.find_element_by_xpath("//*[#id='root']/div/div[2]/div/main/div/div/div/form/div[3]/div/div[2]/div/span")
r.click
The button is never clicked and nothing happens i get no error and i can't click it any help would be appreciated.
You can use css selector below to get span:
r = driver.find_element_by_css_selector(".link_has-no-phone")
r.click()
r = driver.find_element_by_xpath("//*[#id='root']/div/div[2]/div/main/div/div/div/form/div[3]/div/div[2]/div/span")
r.click()
You just forgot the parenthesis
To click on the element with text as I don't have a telephone number you can use either of the Locator Strategies:
css_selector:
driver.find_element_by_css_selector("span.toggle-link.link_has-no-phone").click()
xpath:
driver.find_element_by_xpath("//span[#class='toggle-link link_has-no-phone']").click()
Related
Trying to scrape a website, I created a loop and was able to locate all the elements. My problem is, that the next button id changes on every page. So I can not use the id as a locator.
This is the next button on page 1:
<a rel="nofollow" id="f_c7" href="#" class="nextLink jasty-link"></a>
And this is the next button on page 2:
<a rel="nofollow" id="f_c9" href="#" class="nextLink jasty-link"></a>
Idea:
next_button = browser.find_elements_by_class_name("nextLink jasty-link")
next_button.click
I get this error message:
Message: no such element: Unable to locate element
The problem here might be that there are two next buttons on the page.
So I tried to create a list but the list is empty.
next_buttons = browser.find_elements_by_class_name("nextLink jasty-link")
print(next_buttons)
Any idea on how to solve my problem? Would really appreciate it.
This is the website:
https://fazarchiv.faz.net/faz-portal/faz-archiv?q=Kryptow%C3%A4hrungen&source=&max=10&sort=&offset=0&_ts=1657629187558#hitlist
There are two issues in my opinion:
Depending from where you try to access the site there is a cookie banner that will get the click, so you may have to accept it first:
browser.find_element_by_class_name('cb-enable').click()
To locate a single element, one of the both next buttons, it doeas not matter, use browser.find_element() instead of browser.find_elements().
Selecting your element by multiple class names use xpath:
next_button = browser.find_element(By.XPATH, '//a[contains(#class, "nextLink jasty-link")]')
or css selectors:
next_button = browser.find_element(By.CSS_SELECTOR, '.nextLink.jasty-link')
Note: To avoid DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() import in addition from selenium.webdriver.common.by import By
You can't get elements by multiple class names. So, you can use find_elements_by_css_selector instead.
next_buttons = browser.find_elements_by_css_selector(".nextLink.jasty-link")
print(next_buttons)
You can then loop through the list and click the buttons:
next_buttons = browser.find_elements_by_css_selector(".nextLink.jasty-link")
for button in next_buttons:
button.click()
Try below xPath
//a[contains(#class, 'step jasty-link')]/following-sibling::a
I am trying to scrape a website, but I need to search for an element whose parent is like this:
//div[#title="parent"]
People are talking about getting an element from its child. Is there a way to reverse it and find the child from its parent?
I want the /span with #title = "child" whose parent is //div[#title = "Search results."]
You can try to use
//div[parent::div[#title="parent"]]
or simply
//div[#title="parent"]/div
In Python code you can also use
parent = driver.find_element(By.XPATH, '//div[#title="parent"]')
child = parent.find_element(By.XPATH, './div')
To locate the child element:
<span title="child"...>
within it's parent:
<div title="Search results."...>
you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "div[title='Search results.'] span[title='child']")
Using xpath:
element = driver.find_element(By.XPATH, "//div[#title='Search results.']//span[#title='child']")
I am trying to find the following element in selenium
It is a user name input field and I use: loginLink = driver.find_element(By.name, "loginEmail" but keep getting "no such element" message.
//input[#ng-reflect-name='loginEmail']
Use xpath or CSS , you can find by name only if the attribute key is 'name'
Eg 'name=loginEmail'
driver.find_element_by_xpath("//input[#ng-reflect-name='loginEmail']")
driver.find_element_by_css_selector("input[ng-reflect-name='loginEmail']")
you can use xpath and css for any attribute as
xpath: //tagname[#attriubute='value']
css: tagname[attriubute='value']
Using the xpath given by PDHide the code you need to use is
loginLink = driver.find_element_by_xpath("//input[#ng-reflect-name='loginEmail']")
On this website, I'm trying to find an element based on its XPATH, but the XPATH keeps changing. What's the next best alternative?
Snippet from website
<button class="KnkXXg vHcWfw T1alpA kiPMng AvEAGQ vM2UTA DM1_6g _-kwXsw Mqe1NA SDIrVw edrpZg" type="button" aria-expanded="true"><span class="nW7nAQ"><div class="VpIG5Q"></div></span></button>
XPATH:
//*[#id="__id15"]/div/div/div[1]/div[2]/div
#Sometimes id is a number between 15-18
//*[#id="__id23"]/div/div/div[1]/div[2]/div
#Sometimes id is a number between 13-23
Here's how I use the code:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, """//*[#id="__id3"]/div/div/div[1]/div[2]/div/div/div/button"""))).click()
I've tried clicking the element by finding the button class, but for whatever reason it won't do anything.
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "KnkXXg vHcWfw T1alpA kiPMng AvEAGQ vM2UTA DM1_6g _-kwXsw Mqe1NA SDIrVw edrpZg"))).click()
If Part of the text is keep changing you can use contains in the xpath.
//*[contains(#id,"__id"]/div/div/div[1]/div[2]/div
I looked on every Posts but didnt get the solution i want.
browser.find_element_by_xpath("//*/section/main/div/div[3]/article/div[1]/div/div[1]/div[1]/a")
pic = browser.find_elements_by_xpath("//*/section/main/div/div[3]/article/div[1]/div/div[1]/div[1]/a").get(title)
print(title)
Thats my Code just now but I Need exactly this:
<div class="eLAPa"><div class="KL4Bh"><img class="FFVAD" decoding="auto" sizes="281.671875px" srcset="https://scontent-frx5-1.cdninstagram.com/vp/38a5fdde34937b2b3e4e600da56c46b5/5C0D05B3/t51.2885-15/e15/s150x150/46276509_388170972011907_7609813800358803282_n.jpg 150w,https://scontent-frx5-1.cdninstagram.com/vp/74b222ca252a488bb5eb110347283c3b/5C0CB7F9/t51.2885-15/e15/s240x240/46276509_388170972011907_7609813800358803282_n.jpg 240w,https://scontent-frx5-1.cdninstagram.com/vp/af2eff79d9c38b0f97a763041e3a99e5/5C0D10C3/t51.2885-15/e15/s320x320/46276509_388170972011907_7609813800358803282_n.jpg 320w,https://scontent-frx5-1.cdninstagram.com/vp/bad123f6264636de35489b7d8dc2cbed/5C0CC199/t51.2885-15/e15/s480x480/46276509_388170972011907_7609813800358803282_n.jpg 480w,https://scontent-frx5-1.cdninstagram.com/vp/93a4e36addf453f754e4caba706fe93a/5C0CC82C/t51.2885-15/e15/s640x640/46276509_388170972011907_7609813800358803282_n.jpg 640w" src="https://scontent-frx5-1.cdninstagram.com/vp/93a4e36addf453f754e4caba706fe93a/5C0CC82C/t51.2885-15/e15/s640x640/46276509_388170972011907_7609813800358803282_n.jpg" style=""></div><div class="_9AhH0"></div></div><div class="u7YqG"><div class="Byj2F"><span class="glyphsSpriteVideo_large u-__7" aria-label="Video"></span></div></div>
a href and then Comes a link which i need
To extract the text Video you can use either of the following solutions:
css_selector:
driver.find_element_by_css_selector("a[href='/p/BrDudIUBuNr/'] span.glyphsSpriteVideo_large").get_attribute("aria-label")
xpath:
driver.find_element_by_xpath("//a[#href='/p/BrDudIUBuNr/']//span[contains(#class,'glyphsSpriteVideo_large')]").get_attribute("aria-label")