i was trying to click some button in the page but it changes when it is available so that mine is not working.
basically, normally it's the only one section but changes into one that contains multiple buttons. and i am aiming to click buying or another buttons when it shows but i kept failing.
when it's unavailable(to click buying or shipping button), it looks like this.
<div class="XqRGHcrncz">
<ul class="_3YA58cPPsy">
<li class="_3nAZvQO51p N=a:pcs.mylist">
<a href="javascript:void(0)" role="button" class="_3Dy-2NaoiG" aria-pressed="false">
<span class="_3nBu7xChUl"><span class="blind">찜하기</span></span>
<em class="_1c-2nfzJqH">13</em></a></li></ul></div>
but when it is available, buying button appears. everything is same but the starting from
li class, it changes a bit.
li class became
<li class="_3nAZvQO51p N=a:pcs.mylist">
and the rest changed too.
<a href="javascript:void(0)" class="OgETmrvExa">
<span class="blind">구매하기</span>
how can i make xpath to click the element that shows only available?
the main problem is that div is changing.
xpath is sometimes
//*[#id="content"]/div/div[2]/div[2]/fieldset/div[7]/ul[1]/li/a
but sometimes it is
//*[#id="content"]/div/div[2]/div[2]/fieldset/div[8]/ul[1]/li/a
so that the div[] is changing. i tried css selector to click it when it turned into new page,
buy=driver.find_css_selector(div.XqRGHcrncz)
lists = buy.find_elements_by_tag_name("ul")
if len(lists) == 2: buy.click()
but when the page is loaded to available, it is not working at all...
i was trying to use xpath like this,
while True:
lists = buy.find_elements_by_tag_name("ul")
if len(lists) == 2:
break
else:
print("구매불가")
driver.refresh()
driver.implicitly_wait(10)
and then
driver.implicitly_wait(10)
xpath='//*[#id="content"]/div/div[2]/div[2]/fieldset/div[8]/ul[1]/li/a'
driver.find_element_by_xpath(xpath).click()
but as i mentioned, the xpath is changing and there is no use. the div[number] <- this changes so that it does not working as it is wrong xpath.
what should i do?? i would be really appreciate if anyone helps me.
(just in case, this is my page that i want to click when it became available...
https://smartstore.naver.com/hwaflora/products/5192517936 thank you)
Try to find element by text, is it stable?구매하기
so the code looks like that:
driver.find_element(By.XPATH, "//span[text()='구매하기']"
or by preceding xpath feature:
driver.find_element(By.XPATH, "//a/preceding::span[text()='찜하기']")
The xpath of your element is
String button = "//li[contains(#class,'_3nAZvQO51p N')]/a"
Now, you mentioned that until it is available there are more elements below it, for example this:
String loading = "//li[contains(#class,'_3nAZvQO51p N')]/a//em"
You can wait until this element disappears. The syntax in Java is:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until((ExpectedConditions.invisibilityOfElementLocated(By.xpath(loading))));
Now you can find and click the button element
Related
I am writing a selenium code to go to a website and click on the following element to expand its drop down list:
Now this area is comprised of two elements.
Element 1:
Element code: <b class="icon" id="handler2"></b>
Full Xpath: /html/body/div[4]/div/div/div[3]/div[2]/div[2]/ul/li/ul/li/p/b
Element 2:
Element code: <p class="wrap button draggable" id="anonymous_element_1"><b class="icon" id="handler2"></b>Reports</p>
Full Xpath: /html/body/div[4]/div/div/div[3]/div[2]/div[2]/ul/li/ul/li/p
I used element 1. My selenium code is lengthy but the the line i used to click 'reports' is:
click_button=driver.find_element_by_xpath('/html/body/div[4]/div/div/div[3]/div[2]/div[2]/ul/li/ul/li/p/b').click()
It doesn't click the button though. I'm not sure why. Does anyone know how I can solve this problem?. It is worth noting that if you use element 2, then a double click is required.
Try:
click_button=driver.find_element_by_xpath('//*[#id="handler2"]').click()
Here is the inspect result for the button that says +5 per day
>span class="text user-links entry-method-title ng-scope ng-binding" ng-include="::'views/entry-text/'+entry_method.entry_type+'.html'">
Click For a Daily Bonus Entry"
</span>
<div class="entry-method bonus template" data-remove-popovers="" id="em6129519" ng-class="{expanded: entryState.expanded == entry_method, template: entry_method.template, 'completed-entry-method': !canEnter(entry_method) && isEntered(entry_method)}" ng-repeat="entry_method in ::entry_methods">
here is the HTML given information when I inspect the link/button, I have tried to use XPath, CSS, link text, and class name and it keeps giving me an error saying it cannot identify the element. Does anyone have a suggestion for how to identify this, it is on gleam.io for a giveaway I'm trying to automate this so i don't have to log in and press this everyday. This is my first ever web interfacing project with python.
Here is my most recent try
driver.maximize_window()
time.sleep(10)
driver.execute_script("window.scrollTo(0, 1440)")
time.sleep(10)
button2 = driver.find_element_by_class_name("text user-links entry-method-title ng-scope ng-binding")
button2.click()
Similar to a previous issue, Selenium find_element_by_class_name and find_element_by_css_selector not working, you can't have spaces in your class name when using driver.find_element_by_class_name. Instead, find the element via css_selector and replace each space with a dot.
driver.find_element_by_css_selector("span.text.user-links.entry-method-title.ng-scope.ng-binding")
That'll fix what you have above, but keep in mind there are other ways to make selenium actions more reliable (eg. WebDriverWait, etc). And there may be a cleaner selector to use than the one above.
I believe the element you want to access is contained within an "iframe", thus you must first switch to iframe before you can access it using selectors.
driver.switch_to.frame(x.find_element_by_xpath("PUT IFRAME XPATH HERE"))
I am attempting to scrape Kickstarter based on the project names alone. Using the project name and the base URL I can get to the search page. In order to scrape the project page, I need to use Selenium to click on the URL. However, I cannot point Selenium to the correct element to click on. I would also like this to be dynamic so I do not need to put the project name each time.
<div class="type-18 clamp-5 navy-500 mb3">
<a href="https://www.kickstarter.com/projects/1980119549/knife-block-
designed-by-if-and-red-dot-winner-jle?
ref=discovery&term=Knife%20block%20-
%20Designed%20by%20IF%20and%20Red%20dot%20winner%20JLE%20Design"
class="soft-black hover-text-underline">Knife block -
Designed by IF and
Red dot winner JLE Design
</a>
</div>`
driver = webdriver.Chrome(chrome_path)
url = 'https://www.kickstarter.com/discover/advanced?ref=nav_search&term=Knife
block - Designed by IF and Red dot winner JLE Design'
driver.get(url)
elem = driver.find_elements_by_link_text('Knife block - Designed by IF and Red
dot winner JLE Design')
elem.click()
How can I get the elem to point to the correct link?
In regards to your attempt, your code had a typo: using find_elements.... returns a list of elements so the method .click() would not work. You mean to use find_element.
To dynamically click links, use an XPath instead. The resulting code would be:
elem = driver.find_element_by_xpath('//div[contains(#class, "type-18")]/a')
elem.click()
This would grab the first match. You could do find_elements and iterate over the elements but this would be a bad approach because since you're clicking the links, each time that renders the previous page stale. If there's more than one, you could use the same XPath but indexed:
first_elem = driver.find_element_by_xpath('(//div[contains(#class, "type-18")]/a)[1]')
first_elem.click()
# ...
second_elem = driver.find_element_by_xpath('(//div[contains(#class, "type-18")]/a)[2]')
second_elem.click()
# And so forth...
Here is a copy/paste from the source
<li id="find-tab" data-qa="sidebar.find.tab" class="nav-tab active">
<span class="in"></span>
<p id="find-label">FIND</p></li>
I have tried:
driver.find_element_by_link_text("FIND").click()
and:
driver.find_element_by_css_se("nav-tab").click()
neither is working.
How do I just simply click on the nav button given that source code I pasted above?
As suggested by #Random Davis in the comments as well. You can use :
driver.find_element_by_id('find-label').click();
Note : Please make sure this would work appropriately for the cases when the find-label element is unique in the page source.
In case there are chances of them being in multiple places, you can create and use a list using :
findLink = driver.find_elements_by_id('find-label');
for links in findLink:
print links.click();
# do whatever you want and navigate back to access the next find-label element
The HTML looks like this:
<span class="MenuIcons searchButton"></span>
... (some stuff)
<a data-bind="" url="/ParagonLS/Search/Property.mvc/Index/1" tabdescription="RESIDENTIAL" subtabdescription="Criteria" subtabmaxallowed="3" targetex="" rel="" class=" SearchByClass1 " subtabgroup="true" subtabgroupadd="true" subtabstartindex="0" fullwindow="False" hideaddressbar="False">TEXT</a>
I can get to the span using:
driver.find_element_by_css_selector(".MenuIcons.searchButton")
But since the span is a drop down menu I need to get to the inner element, but don't know how since it has spaces around its class name. What do I do?
import time
driver.find_element_by_css_selector(".MenuIcons.searchButton").click()
time.sleep(1)
driver.find_element_by_partial_link_text("TEXT").click()
You can do this and click the link.
I suggest you to use xpath instead since the class contains space.
//a[contains(#class,'SearchByClass1')]
Text based search is also another possibility.
//a[.='TEXT']
Edit
Executing javascript since the element is hidden as per OP's comment
test = driver.execute_script("return document.querySelector(\"a[class*='SearchByClass1']\").innerHTML;");
print(test)
print
TEXT