last day (17 days later) » 

2:03 PM
0
A: python selenium how to deal with absence of an element

Breaks Softwareif (gt.size() == 0): insert code here to extract the english comments

 
user4069366
Ive edited my code to show mixed reviews. it traverses starting from page 56 , which has english & non-english comments. But if you run the code english gets skipped
 
I don't see any edit in save_comments() that does anything if gt.size() == 0. So anything that doesn't have that translate button will get skipped. You need to add the code to handle this condition.
 
user4069366
Where shall I put gt.size()==0 ? Also how can I know the numeric position of that element for which gt.size()==0 ?
 
Um, you would put it after the "for" loop inside save_comments(). that's where you are trying to save the comments, right?
 
user4069366
still not working. Updated my code, pls check
 
2:03 PM
you don't want it WITHIN the for loop, you want it AFTER the for loop.
 
user4069366
Its not working.Can you pls try it at your end before posting here.
 
user4069366
2:19 PM
hi
 
user4069366
2:34 PM
?
 
2:47 PM
Shalini, I apologize that I didn't check the syntax. I'm using Java right now, so I lean toward methods rather than functions. With that said, I kind of expected you should be able to fix the syntax of this simple statement yourself. perhaps you don't understand the objective?
if (len(gt) == 0):
that will address the syntax.
 
3:14 PM
But this is only part of the challenge. You'll still need to go find the reviews that don't have the translation link (is that sufficient to find your english reviews? I'm not going to spend the time looking at trip advisor, but if you provide the target html, I can help you come up with a css selector
 
user4069366
ok
 
user4069366
actually if you just look at this page --> tripadvisor.com/…
 
user4069366
You can see that first few reviews are english & then there are portuguese (with "google-translate")
 
user4069366
This is the crucial page (page number 56 that is ) where my scraper breaks. I guess Im hitting the wall, this thing is so so frustrating..
 
user4069366
3:31 PM
BTW I already have used if (len(gt) == 0): , that also isnt working
 
6:57 PM
What is broken with the "if" statement?
I looked at that link you sent to me. this is the approach I would take: use find_elements to find all of the reviews (CSS would be something like "div.reviewSelector", so that we can use the id of that div to uniquely id the review).
Then for each review, I would check if there's a translation button: the_review_div.find_elements(By.CSS_SELECTOR,"div.googleTranslation > span.link") and use your code to pull the translation.
if there is no translation button, then have some other code to pull the english review (it looks like the page you sent me is only providing portion of the review, so you can't just do a find_element to pull it out.
 

  last day (17 days later) »