I'm trying to automatically navigate web pages using python, selenium, xpath.
I want to click a next page button in a page whose code is like this:
<a _ngcontent-pnd-c43="" automation-id="discover-market-next-button"
class="menu-item-button ng-star-inserted">
<span _ngcontent-pnd-c43="" class="nav-button-right sprite"></span>
</a>
I tried with the following code:
try:
element='//span[class="nav-button-right sprite"]'
button_next = driver.find_element_by_xpath(element)
webdriver.ActionChains(driver).move_to_element(button_next).click(button_next).perform()
time.sleep(15)
content = driver.page_source.encode('utf-8')
except NoSuchElementException:
print ("NoSuchElementException")
but I got "NoSuchElementException".
Could anyone help me?
The problem is that nav-button-right sprite is not a class, not even an attribute name. So try changing you xpath expression from:
element='//span[class="nav-button-right sprite"]'
to:
element='//span[#_ngcontent-pnd-c43]'
and see if it works.
You less # in:
element='//span[#class="nav-button-right sprite"]'
I think the problem is in your XPath, you missed # symbol after class, try this hope it helps:
element='//span[#class='nav-button-right sprite']'
Related
Which method in Selenium with python can be used to click on the following buttons?
Button nr. 1:
<a href="/help.html"> = $0
<i class="fa fa-question" aria-hidden="true"></i> HELP </a>
Button nr.2:
<a class="active-menu-item" href="/help/making_service_requests.html">Service Requests</a>
Button nr.3:
Transfer Service
I have already tried but wont work:
driver.find_element_by_xpath('//*[#id="topmenuheader"]/div/ul/li[3]/a').click()
driver.find_elements_by_partial_link_text("Transfer Service").click()
driver.find_element_by_class_name("btn").click()
I have encountered many instances where there are no id, no class etc, butand href available or an href with an html reference as mentioned above. These seem to be tricky as i have tried quite a few things. Maybe some of you have encountered these and could help out. Thanks
These should work for those 3 elements. I showed multiple ways of getting those a tags either by href, class or text.
driver.find_element_by_xpath("a[#href='/help.html']").click()
driver.find_element_by_xpath("a[#class='active-menu-item']").click()
driver.find_element_by_xpath("a[text()='Transfer Service']").click()
You can do this :
Driver = driver.find_element_by_xpath("path here")
Driver.click()
I want to get some links in the url but they I get all links instead. How can I pull the links by specifying the selector?
For ex:
I'm using:
ids = browser.find_elements_by_xpath('//a[#href]')
for ii in ids:
print(ii.get_attribute('href'))
Result: All Links
But I want just some selector
<a class="classifiedTitle" title="MONSTER TULPAR T5V13.1+ 15,6 EKRAN+İ7+6GB GTX1060+16RAM+256GB SS" href="/ilan/ikinci-el-ve-sifir-alisveris-bilgisayar-dizustu-notebook-monster-tulpar-t5v13.1-plus-15%2C6-ekran-plusi7-plus6gb-gtx1060-plus16ram-plus256gb-ss-793070526/detay">
MONSTER TULPAR T5V13.1+ 15,6 EKRAN+İ7+6GB GTX1060+16RAM+256GB SS</a>
So how can I add some selectors?
Thanks & Regards
Try the following css selector to get the specific link.
print(browser.find_element_by_css_selector("a.classifiedTitle[title='MONSTER TULPAR T5V13.1+ 15,6 EKRAN+İ7+6GB GTX1060+16RAM+256GB SS'][href*='/ilan/ikinci-el-ve-sifir-alisveris-bilgisayar-dizustu-notebook-monster-tulpar-']").get_attribute("href"))
If you want just the item in your example:
href=browser.find_element_by_xpath("//a[#title='MONSTER TULPAR T5V13.1+ 15,6 EKRAN+İ7+6GB GTX1060+16RAM+256GB SS" href="/ilan/ikinci-el-ve-sifir-alisveris-bilgisayar-dizustu-notebook-monster-tulpar-t5v13.1-plus-15%2C6-ekran-plusi7-plus6gb-gtx1060-plus16ram-plus256gb-ss-793070526/detay']").get_attribute('href')
There are obviously more than one way of identifying your element, this is simply an example using xpath.
i'm trying to pick up links of youtube channels which are located as below:
<a id="author-text" class="yt-simple-endpoint style-scope ytd-comment-
renderer" href="/channel/UCUSy-h1fPG1L6X7KOe70asA"> <span class="style-
scope ytd-comment-renderer">Jörgen Nilsson</span></a>
So in the example above I would want to pick up "/channel/UCUSy-h1fPG1L6X7KOe70asA". So far i have tried many options but none work:
driver = webdriver.Chrome('C:/Users/me/Chrome Web Driver/chromedriver.exe')
api_url="https://www.youtube.com/watch?v=TQG7m1BFeRc"
driver.get(api_url)
time.sleep(2)
div = driver.find_element_by_class_name("yt-simple-endpoint style-scope ytd-comment-renderer")
but I get the following error:
InvalidSelectorException: Message: invalid selector: Compound class names not permitted
I also tried other approaches:
div = driver.find_elements_by_xpath("yt-simple-endpoint style-scope ytd-comment-renderer")
div = driver.find_element_by_class_name('yt-simple-endpoint style-scope ytd-comment-renderer')
div=driver.find_element_by_css_selector('.yt-simple-endpoint style-scope ytd-comment-renderer').get_attribute('href')
but no luck.. if someone could please help it would be much appreciated. Thank you
Your selectors are invalid:
driver.find_element_by_class_name("yt-simple-endpoint style-scope ytd-comment-renderer")
you cannot pass more than one class name to find_element_by_class_name method. You can try driver.find_element_by_class_name("ytd-comment-renderer")
driver.find_elements_by_xpath("yt-simple-endpoint style-scope ytd-comment-renderer")
it's not a correct XPath syntax. You probably mean driver.find_elements_by_xpath("//*[#class='yt-simple-endpoint style-scope ytd-comment-renderer']")
driver.find_element_by_css_selector('.yt-simple-endpoint style-scope ytd-comment-renderer')
each class name should start with the dot: driver.find_element_by_css_selector('.yt-simple-endpoint.style-scope.ytd-comment-renderer')
But the best way IMHO to identify by ID:
driver.find_element_by_id("author-text")
You can use BeautifulSoup in python to get the links in anchor tag having specific class names like soup.find_all('a', attrs={'class':'yt-simple-endpoint'}) you can read more here find_all using css
I am very new to Selenium and Python in general. I want to get the title of a td class, the HTML code looks like this. I only need to get the 6,012,567 number to use later:
<td class="infoStats__stat link-light border-light-right">
<a href=“/follow" class="infoStats__statLink link-light" title="6,012,567">
<h3 class="infoStats__title font-light”>Users</h3>
<div class="infoStats__value font-tabular-light">6.01M</div>
</a>
</td>
So far I have this:
#element = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.XPATH, '//td[contains(#class, "infoStats__statLink")]')))
#users = int(element.text.replace(',', ''))
But this is just giving me the 6.01 abbreviation, is there a better way to do this?
Use following code to get required value:
value = int(driver.find_element_by_xpath('//a[#class="infoStats__statLink link-light"]').get_attribute("title"))
Let me know if any errors occurs
Use the below XPath:-
//a[#class='infoStats__statLink link-light']/#title
Use code as below :-
elem = driver.find_elements_by_xpath("//a[#class='infoStats__statLink link-light']/#title");
print elem.text
Hope it will help you :)
All I want is to close a modal dialog, ideally by doing the following:
browser.find_element_by_link_text("OK").click()
Gives NoSuchElementException: Message: u'The element could not be found' for the OK link text.
Same for the xpath when I do this:
browser.find_element_by_xpath("//*[#id=\"modal\"]/div/div[2]/div/a").click()
I suspect this is because I need to put focus on the dialog. To do so I've tried:
for handle in browser.window_handles:
browser.switch_to_window(handle)
if browser.find_element_by_class_name('popUp123')
browser.find_element_by_link_text("OK").click()
Gives NoSuchElementException: Message: u'The element could not be found' for the class.
Have also tried browser.switch_to_frame(ID OR NAME), but couldn't find it as a frame either.
Please tell me I'm missing something blatantly obvious.
Relevant frame source (summarised):
<body id="modal">
<div class="popUp123">
<div class="button">
<div class="centerbutton">
<a href="#" class="close" onclick=parent.close">
<span>OK</span>
This is the python syntax
from selenium.webdriver.remote.webdriver import WebDriver
browser = WebDriver()
# do other stuff here
browser.switch_to_alert().accept()
# continue with other stuff here
The alert api is located in selenium.webdriver.common.alert
The below code is using Java, u can try using the below code converting it to Python syntax. Sorry as I am Webdriver - Java Tester, I can't give you the Python code. Hope this will solve your requirements.
Alert alertDialog = driver.switchTo().alert();
//Get the alert text
String alertText = alertDialog.getText();
//Click the OK button on the alert.
alertDialog.accept();
Cheers,
Mahesh